# 8.8.2. Medium Multiple Choice QuestionsΒΆ

These problems are similar to those you will see on the AP CSA exam.

- 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.

8-8-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.

8-8-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.

8-8-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.

You have attempted of activities on this page