# Medium Multiple Choice Questions¶

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

9-8-1: 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];
}

• (A) 4
• This would be correct if the variable col was 0 because then it would add 1 + 1 + 1 + 1 which is 4.
• (B) 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.
• (C) 9
• This would be correct if the variable col was 1 because then it would add 1 + 2 + 2 + 4 which is 9.
• (D) 12
• This would be correct if the variable col was 3 becuase then it would add 2 + 4 + 4+ 2 which is 12.
• (E) 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.

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

9-8-2: 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; } }

• (A) {{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.
• (B) {{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.
• (C) {{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.
• (D) {{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.
• (E) {{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.

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

9-8-3: 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];
}

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

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

Next Section - Hard Multiple Choice Questions