AP® Computer Science A Data Collections Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: 2D Arrays Clear filter
Question 132
Question 133
int[][] m =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
What is printed by the following code segment?
int total = 0;
for (int r = 0; r < m.length; r++)
{
total += m[r][m[r].length - 1 - r];
}
System.out.println(total);Main Solution
Solution:
Correct: D. This computes the anti-diagonal sum. r=0: m[0][3]=4. r=1: m[1][2]=7. r=2: m[2][1]=10. r=3: m[3][0]=13. Total = 4 + 7 + 10 + 13 = 34.
Answer: Correct: D
Answer images
Question 134
int[][] g =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int r = g.length - 1; r > 0; r--)
{
g[r][0] = g[r - 1][0];
}
for (int r = 0; r < g.length; r++)
{
System.out.print(g[r][0] + " ");
}
What is printed?Main Solution
Solution:
Correct: D. The loop shifts column 0 downward (propagating upstream values). Starting column 0 = [1, 4, 7]. r=2: g[2][0]=g[1][0]=4 → column 0 = [1, 4, 4]. r=1: g[1][0]=g[0][0]=1 → column 0 = [1, 1, 4]. Output: 1 1 4.
Answer: Correct: D
Answer images
Question 135
int[][] grid = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int sum = 0;
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[0].length; c++)
{
if (r == c || r + c == grid[0].length - 1)
{
sum = sum + grid[r][c];
}
}
}
What value is stored in sum after the code segment executes?Main Solution
Solution:
Answer: C. The condition admits cells on the main diagonal (r == c) and on the anti-diagonal (r + c == cols - 1 == 3). Main diagonal: grid[0][0] = 1, grid[1][1] = 6, grid[2][2] = 11 — sum 18. Anti-diagonal: grid[0][3] = 4, grid[1][2] = 7, grid[2][1] = 10 — sum 21. Because the grid is not square (3×4), the two diagonals share no cell, so double counting cannot occur. Total 18 + 21 = 39.
Answer: Correct: C
Answer images
Question 136
int[][] m = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
int[][] r = new int[m[0].length][m.length];
for (int i = 0; i < m.length; i++)
{
for (int j = 0; j < m[0].length; j++)
{
r[j][i] = m[i][j];
}
}
What 2-D array is stored in r after the code segment executes?Main Solution
Solution:
Answer: D. The code computes the transpose of m. For each source cell m[i][j], the value is written to r[j][i], swapping the row and column indices. The original m is 2×3, so r is allocated 3×2. Reading off the result: r[0] = { m[0][0], m[1][0] } = { 1, 4 }; r[1] = { m[0][1], m[1][1] } = { 2, 5 }; r[2] = { m[0][2], m[1][2] } = { 3, 6 }.
Answer: Correct: D
Answer images
Question 137
Question 138
public static int f(int n)
{
if (n <= 1)
{
return n;
}
return n + f(n - 2);
}
What is returned by the call f(7)?Main Solution
Answer
Answer: B. The call unwinds as f(7) = 7 + f(5) = 7 + 5 + f(3) = 7 + 5 + 3 + f(1). Since 1 <= 1, f(1) = 1. Total = 7 + 5 + 3 + 1 = 16.
Answer: Correct: B
Answer images
Question 139
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

