Second Summer 2002
Arrays (Chapter 11): The warm-ups.
- For each of the following sets of values, write code that fills an array
a with the values:
Use a loop when appropriate.
1 2 3 4 5 6 7 8 9 10
0 2 4 6 8 10 12 14 16 18
1 4 9 16 25 36 49 64 81 100
0 0 0 0 0 0 0 0 0 0
1 4 9 16 9 7 4 9 11
- 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.
- Write Java code for a loop that simultaneously computes the maximum and the minimum of an array.
- What is wrong with the following loop?
Explain two ways of fixing the error.
int v = new int;
for (int i = 1; i <= 10; i++) v[i] = i * i;
- What is an array index? What are the bounds of an array?
- Give an example of
Just describe each method. Don't implement the methods.
- A useful method that has an array of integers as a parameter that is not modified
- A useful method that has an array of integers as a parameter that is modified
- A useful method that has an array of integers as a return value
- 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.
- What are parallel arrays? Why are parallel arrays indications of poor programming?
How can they be avoided?
- Design a class
Catalog that stores a collection of products. What public
methods should you support? What advantages and disadvantages does a
have over a
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.
- How do you perform the following tasks with arrays in Java?
- Test that two arrays contain the same elements in the same order.
- Copy one array to another.
- Fill an array with zeroes, overwriting all elements in it.
- Remove all elements from an array.
- True or false?
- All elements of an array are of the same type.
- Array subscripts must be integers.
- Arrays cannot contain strings as elements.
- Arrays cannot use strings as subscripts.
- Parallel arrays must have equal length.
- Two-dimensional arrays always have the same numbers of rows and columns.
- Two parallel arrays can be replaced by a two-dimensional array.
- Elements of different columns in a two-dimensional array can have different types.
- Elements in a vector can have different types.
- True or false?
- A method cannot return a two-dimensional array.
- A method can change the length of an array parameter.
- A method cannot change the dimensions of a two-dimensional array parameter.
- A method cannot change the length of a vector that is passed as a parameter.
- 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