Second Summer 2002


Arrays (Chapter 11): The warm-ups.

Questions:

  1. For each of the following sets of values, write code that fills an array a with the values: Use a loop when appropriate.

  2. Write a loop that fills an array a with ten random numbers between 1 and 100. Write code for two nested loops that fill a with ten different random numbers between 1 and 100.

  3. Write Java code for a loop that simultaneously computes the maximum and the minimum of an array.

  4. What is wrong with the following loop?
    int[] v = new int[10];
    for (int i = 1; i <= 10; i++) v[i] = i * i;
    Explain two ways of fixing the error.

  5. What is an array index? What are the bounds of an array?

  6. Give an example of

    1. A useful method that has an array of integers as a parameter that is not modified

    2. A useful method that has an array of integers as a parameter that is modified

    3. A useful method that has an array of integers as a return value
    Just describe each method. Don't implement the methods.

  7. A method that has an array as a parameter can change the array in two ways. It can change the contents of individual array elements, or it can rearrange the elements. Describe two useful methods with Product[] parameters that change an array of products in each of the two ways just described.

  8. What are parallel arrays? Why are parallel arrays indications of poor programming? How can they be avoided?

  9. Design a class Catalog that stores a collection of products. What public methods should you support? What advantages and disadvantages does a Catalog class have over a Product[] array?

  10. Suppose v is a sorted vector of products. Describe how a new product can be inserted in its proper position so that the resulting vector stays sorted.

  11. How do you perform the following tasks with arrays in Java?

    1. Test that two arrays contain the same elements in the same order.

    2. Copy one array to another.

    3. Fill an array with zeroes, overwriting all elements in it.

    4. Remove all elements from an array.

  12. True or false?

    1. All elements of an array are of the same type.

    2. Array subscripts must be integers.

    3. Arrays cannot contain strings as elements.

    4. Arrays cannot use strings as subscripts.

    5. Parallel arrays must have equal length.

    6. Two-dimensional arrays always have the same numbers of rows and columns.

    7. Two parallel arrays can be replaced by a two-dimensional array.

    8. Elements of different columns in a two-dimensional array can have different types.

    9. Elements in a vector can have different types.

  13. True or false?

    1. A method cannot return a two-dimensional array.

    2. A method can change the length of an array parameter.

    3. A method cannot change the dimensions of a two-dimensional array parameter.

    4. A method cannot change the length of a vector that is passed as a parameter.

    5. A method can only reorder the elements of an array parameter, not change the elements.

Here's where the solutions are.


Last updated: Jun 16, 2002 by Adrian German for A201