You are on page 1of 4

Quiz 09: Solution

1) The following code accomplishes which of the tasks written below? Assume list
is an int array that stores positive int values only.
int foo = 0;
for (int j =0 ; j < list.length; j++)
if (list[j] > foo) foo = list[j];
a) it stores the smallest value in list (the minimum) in foo
b) it stores the largest value in list (the maximum) in foo
c) it stores every value in list, one at a time, in foo, until the loop terminates
d) it counts the number of elements in list that are greater than foo
e) it counts the number of elements in list that are less than foo

Answer: b. Explanation: The condition in the if statement tests to see if the current
element of list is greater than foo. If so, it replaces foo. The end result is that every
element in list is tested and foo stores the largest element up to that point, so eventually,
foo will be the largest value in the array list.

2) The statement int[ ] list = {5, 10, 15, 20};


a. initializes list to have 20 int values
b. initializes list to have 4 int values
c. declares list but does not initialize it
d. causes a syntax error because it does not include new int[4] prior to the
list of values

Answer: b. Explanation: An array does not have to be instantiated with the new
reserved word if it is instantiated with the list of values it is to store. So, list = {5, 10, 15,
20}; causes list to become an array of 4 int values with the values of 5, 10, 15, 20 already
initialized. Note that the array is automatically an array of 4 values, so list[n] for any
value of n other than 0, 1, 2 or 3 would result in a thrown Exception.

3) To initialize a String array names to store the three Strings Huey, Duey and
Louie, you would do
a. String names = {Huey, Duey, Louie};
b. String[ ] names = {Huey, Duey, Louie};
c. String[ ] names = new String{Huey, Duey, Louie};
d. String names[3] = {Huey, Duey, Louie};
e. String names; names[0] = Huey; names[1] = Duey; names[2] =
Louie;

Answer: b. Explanation: An array does not have to be instantiated with the reserved
word new if it is instantiated with the list of values it is to store. So, names =
{Huey, Duey, Louie}; will create a String array of 3 elements with the three

1
values already initialized. Of the other answers, (a) does not specify that names is a
String array, (c) should not have the reserved word new, (d) should not have [3] after
names and omits [ ] after String, and (e) does not instantiate the array as String[3],
and thus , all four of these other answers are syntactically invalid.

4) What will be the value of x after the following code is executed:


int x = 0;
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k--)
x++;
Answer: 10,000. Explanation: The outer loop runs 100 times. For each run, the inner
loop runs 100 times. This makes the statement x++; to be executed 10,000 times. The
initial value of x is 0, and it is incremented 10,000 times

5) Given the declaration int[][] a = new int [rows][columns], find the logical error in
the code below and correct it
for( i = 0; i < columns; i++)
for(j = 0; j < rows; j ++)
a[i][j]++;

The first index in a[i][j] always shows the rows, the second shows the columns.
The outer loop with index variable i should run up to rows, the inner up to
columns:
for( i = 0; i < rows; i++)
for(j = 0; j < columns; j ++)
a[i][j]++;

Equivalent code:

for( i = 0; i < columns; i++)


for(j = 0; j < rows; j ++)
a[j][i]++;

2
6) Given the declaration, does the statement below correctly print the values of the
array?

int [] alpha = {100, 200, 300, 400, 500};

for (int k = 0; k < 5; k++)


System.out.println (alpha);

Answer: no. Explanation: alpha contains the address of the memory location where
the elements of the array are stored. Printing alpha will result in printing the address
five times, not the elements of the array.

7) Which code below correctly increments all the elements in array alpha declared
as int [][] alpha = new int [rows][columns]; (there may be more than
one correct answer)

a. for (int k = 0; k < columns; k ++)


for(int j = 0; j < rows; j++)
alpha[k][j]++;

b. for (int k = 0; k < rows; k++)


for(int j = 0; j < rows; j++)
alpha[k][j]++;

c. for(int k = 0; k < rows; k++)


for(int j = 0; j < columns; j++)
alpha[j][k]++;

d. for(int k = 0; k < columns; k++)


for(int j = 0; j < rows; j++)
alpha[j][k]++;

e. for (int k = 0; k < rows; k ++)


for(int j = 0; j < columns; j++)
alpha[k][j]++;

Answer d and e. Explanation: In (d) the first index j changes up to rows and the
second index k changes up to columns, which is correct

in (e) the k variable changes up to rows, and it is used as the first index in alpha[k][j].
The j variable changes up to columns and it is used as the second index in alpha[k][j].

3
The difference between d) and e) is that in case d) the elements are incremented column
by column, while in e) the elements are incremented row by row.

Case (a) is not correct because k changes up to columns, and it should be up to rows,
and j changes up to rows, while it should be up to columns

Case (b) is not correct because j changes up to rows, and it should be up to columns

Case c) is not correct because the first index j in alpha[j][k] changes up to columns, and
the second index changes up to rows. It should be exactly the opposite.

You might also like