# 9.6. Multiple Choice Exercises¶

## 9.6.1. Easier Multiple Choice Questions¶

- 2
- The size of outer array is the number of rows. Remember that two-dimensional arrays are actually an array of arrays in Java.
- 4
- The size of the inner array is the number of columns.
- 8
- This is the total number of items in the array.

9-6-1: How many columns does `a`

have if it is created as follows `int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};`

?

You can see how the array looks by clicking on the following Ex-9-7-1.

`strGrid[0][2] = "S";`

- The code
`letterGrid[0][2] = "S";`

actually sets the 1st row and 3rd column to hold a reference to the`String`

object "S". `strGrid[1][3] = "S";`

- This would be true if row and column indicies started at 1 instead of 0 and if this was in column major order.
`strGrid[3][1] = "S";`

- This would be true if row and column indicies started at 1 instead of 0.
`strGrid[2][0] = "S";`

- In row-major order the row is specified first followed by the column. Row and column indicies start with 0. So
`letterGrid[2][0]`

is the 3rd row and 1st column. `strGrid[0][0] = "S";`

- This would set the element at the first row and column.

9-6-2: Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named `strGrid`

(assuming row-major order).

- a[0][3]
- This would be true if the row index started at 0, but the column index started at 1.
- a[1][3]
- Both the row and column indicies start with 0.
- a[0][2]
- The value 6 is at row 0 and column 2.
- a[2][0]
- The row index is specified first, then the column index.
- a[3][1]
- The row index is specified first and the indicies start at 0.

9-6-3: How would you get the value 6 out of the following array `int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};`

?

## 9.6.2. Medium Multiple Choice Questions¶

- 4
- This would be correct if the variable col was 0 because then it would add 1 + 1 + 1 + 1 which is 4.
- 8
- Since col is matrix[0].length - 2 it is 4 - 2 which is 2. This code will loop through all the rows and add all the numbers in the third column (index is 2) which is 2 + 2 + 3 + 1 which is 8.
- 9
- This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
- 12
- This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4+ 2 which is 12.
- 10
- This would be true if we were adding the values in the 3rd row (row = 2) instead of the 3rd column. This would be 1 + 2 + 3 + 4 which is 10.

9-6-4: Given the following code segment, what is the value of sum after this code executes?

```
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0;
int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
```

You can step through this code using the following link Example-9-8-1.

- { {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
- This woud be true if the code put a 3 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 1 in the array when the row index is greater than the column index.
- { {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
- This code will put a 1 in the array when the row index is less than the column index and a 2 in the array when the row and column index are the same, and a 3 in the array when the row index is greater than the column index.
- { {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
- { {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.
- { {1 1 1 1}, {2 2 2 2}, {3 3 3 3}}
- This code creates a 2D array with 4 rows and 3 columns so this can't be right.

9-6-5: What are the contents of `mat`

after the following code segment has been executed?

```
int [][] mat = new int [4][3];
for (int row = 0; row < mat.length; row++) {
for (int col = 0; col < mat[0].length; col++) {
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3; } }
```

You can step through this code using the following link Example-9-8-2.

- 4
- This would be correct if it was adding up all the values in the first row. Does it?
- 6
- This would be correct if it was adding up all the values in column 0.
- 9
- This adds all the values in column 1 starting with the one in the last row (row 3).
- 10
- This would be correct if it was adding up all the values in the second row.
- 20
- This would be correct if it was adding up all the values in the last row.

9-6-6: Given the following code segment, what is the value of sum after this code executes?

```
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}};
int sum = 0;
for (int k = 0; k < m.length; k++) {
sum = sum + m[m.length-1-k][1];
}
```

You can step through this code using the following link Example-9-8-3.

## 9.6.3. Hard Multiple Choice Questions¶

- { {6, 4, 2}, {2, 4, 6}}
- Check the starting values on the nested loops.
- { {3, 2, 1}, {1, 4, 6}}
- Notice that there are two if's, not an if and else.
- { {3, 2, 1}, {1, 4, 8}}
- The first if will change an odd number to an even. The second if will also execute after an odd number has been made even. Both loops start at index 1 so this only changes the items in the second row and second and third column.
- { {4, 4, 2}, {2, 4, 4}}
- Both if's will execute. Also, check the bounds on the nested loop.
- { {3, 2, 1}, {2, 4, 4}}
- Both if's will execute. Check the bounds on the inner loop. When does it stop?

9-6-7: What are the contents of `arr`

after the following code has been executed?

```
int[][] arr = { {3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++) {
for (int col = 1; col < arr[0].length; col++) {
if (arr[row][col] % 2 == 1)
{
arr[row][col] = arr[row][col] + 1;
}
if (arr[row][col] % 2 == 0)
{
arr[row][col] = arr[row][col] * 2;
}
}
}
```

To step through this code in the Java Visualizer click on the following link: Hard 1.

- The maximum brightness value for all pixels in imagePixel
- The method works by scanning all the pixels in imagePixels and comparing them to the current iMax value. If the current is greater, it replaces iMax and becomes the new maximum brightness. This is the value that is returned.
- The column with the greatest brightness sum
- This could be accomplished by adding the brightness in the second loop and comparing the sum to iMax after the second loop finishes and before the first loop starts again.
- The most frequent brightness value in imagePixels
- To do this you would need a third loop and an array, 256 in size. In the second loop you would track how many pixels of a certain brightness had occurred using, countBright[i]++, and then in the third loop find the item in countBright with the highest value.
- The row with the greatest brightness sum
- Firstly, you would need to traverse the 2D array in the opposite order, going through the rows instead of the columns. Then, you would sum each row's brightness in the second loop and compare it to the max in the first loop.
- The sum of the total brightness of imagePixels
- This would be accomplished by instead of having an if statement to track the currentmax, simply using, sum += imagePixels[r][c];

9-6-8: A two-dimensional array, `imagePixels`

, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?

1 2 3 4 5 6 7 8 9 10 11 12 13 | ```
public int findMax(int[][] imagePixels) {
int r, c;
int i, iMax = 0;
for (r = 0; r < imagePixels.length; r++) {
for (c = 0; c < imagePixels[0].length; c++) {
i = imagePixels[r][c];
if (i > iMax)
iMax = i;
}
}
return iMax;
}
``` |