AP® Computer Science A Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions unit by unit, 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.
Question 2572
Question 2573
Question 2574
Question 2575
Question 2576
Question 2577
Question 2578
What is printed?
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4); list.add(4); list.add(3);
list.add(4); list.add(4);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 4) {
list.remove(i);
}
}
System.out.println(list);Main Solution
To determine what is printed, trace through the code step by step.
Initial list: [4, 4, 3, 4, 4] (indices 0–4)
Loop iteration by iteration:
- i = 0:
list.get(0)is 4, solist.remove(0)is called. List becomes [4, 3, 4, 4].iincrements to 1. - i = 1:
list.get(1)is 3 (not 4), so nothing is removed.iincrements to 2. - i = 2:
list.get(2)is 4, solist.remove(2)is called. List becomes [4, 3, 4].iincrements to 3. - i = 3:
list.size()is now 3, so3 < 3is false. Loop ends.
Final list: [4, 3, 4]
System.out.println(list) prints [4, 3, 4], which matches option B.
The key insight is that when an element is removed at index i, all subsequent elements shift left by one position, but i still increments. This causes every other matching element to be skipped.
Teacher Explanation
This question tests students' understanding of the pitfall of removing elements from an ArrayList while iterating over it with an index-based for loop. When list.remove(i) is called, the element at index i is deleted and all elements after it shift left. Since i then increments, the element that shifted into position i is never checked. This causes alternating 4s to be skipped, leaving [4, 3, 4] in the list.
Education Coach Advice
When you see an ArrayList being modified inside a for loop, always trace through the loop manually, updating the list contents and indices after each removal. Remember: removing at index i shifts everything after it left by 1, so the next element effectively moves to index i — but the loop still increments i, causing a skip. This is a classic AP CSA trap.
Traps
Option A ([]) is tempting if students assume all 4s are removed, but the loop skips elements due to index shifting after each removal.
Option C ([3]) is similarly wrong — students might think all 4s are eventually removed, but the skipping behavior prevents that.
Option D ([4, 4]) might be chosen if students miscount which elements are skipped. The correct trace shows that the first and third 4s (at original indices 0 and 2) are removed, leaving the second 4 (original index 1 — but wait, that's a 3), the fourth 4 (original index 3), and the fifth 4 (original index 4) — but the loop exits before checking index 3 again after the second removal.
Answer images
Question 2579
What is printed?
int[][] grid = {{3, 1, 4, 1},
{5, 9, 2, 6},
{5, 3, 5, 8}};
int maxCol = 0;
int maxSum = 0;
for (int c = 0; c < grid[0].length; c++) {
int colSum = 0;
for (int r = 0; r < grid.length; r++) {
colSum += grid[r][c];
}
if (colSum > maxSum) {
maxSum = colSum;
maxCol = c;
}
}
System.out.println(maxCol + " " + maxSum);Main Solution
To determine what is printed, trace through the code by computing the column sums of the 2D array:
| Column (c) | grid[0][c] | grid[1][c] | grid[2][c] | colSum |
|---|---|---|---|---|
| 0 | 3 | 5 | 5 | 13 |
| 1 | 1 | 9 | 3 | 13 |
| 2 | 4 | 2 | 5 | 11 |
| 3 | 1 | 6 | 8 | 15 |
Now trace the outer loop:
- c = 0: colSum = 13. Since 13 > 0 (maxSum), maxSum = 13, maxCol = 0.
- c = 1: colSum = 13. Since 13 > 13 is false, no update. maxSum = 13, maxCol = 0.
- c = 2: colSum = 11. Since 11 > 13 is false, no update.
- c = 3: colSum = 15. Since 15 > 13 is true, maxSum = 15, maxCol = 3.
After the loop, maxCol = 3 and maxSum = 15.
System.out.println(maxCol + " " + maxSum) prints: 3 15.
The correct answer is A.
Teacher Explanation
This question tests 2D array traversal and accumulation. The outer loop iterates over columns (c) and the inner loop iterates over rows (r), summing each column. The code tracks the column index with the maximum sum. Column 3 has the highest sum (15), so the output is 3 15.
Education Coach Advice
When tracing 2D array problems on the AP exam, always build a quick table of row/column sums. Pay close attention to whether the loop iterates over rows first or columns first — here the outer loop is over columns (c) and the inner is over rows (r), which is the reverse of the more common row-major traversal. Also note that the > (strict greater-than) means ties do NOT update, so the first column to achieve a given max keeps the index.
Traps
Common traps:
- Option B (0 13): A student might stop after the first column update and forget that column 3 later produces a higher sum of 15.
- Option C (1 13): A student might confuse the column index or think column 1 wins because it also sums to 13 (but column 0 updates first, and neither beats column 3).
- Option D (3 13): A student might correctly identify column 3 as the winner but miscalculate its sum as 13 instead of 15 (perhaps confusing it with columns 0 or 1).
Answer images
Question 2580
What is printed?
int[][] m = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
int sum = 0;
for (int r = 0; r < m.length; r++) {
for (int c = 0; c < m[0].length; c++) {
if ((r + c) % 2 == 0) sum += m[r][c];
}
}
System.out.println(sum);Main Solution
The code sums all elements m[r][c] where (r + c) % 2 == 0, i.e., where the sum of the row and column indices is even.
The 2D array is:
| c=0 | c=1 | c=2 | c=3 | |
|---|---|---|---|---|
| r=0 | 1 | 2 | 3 | 4 |
| r=1 | 5 | 6 | 7 | 8 |
| r=2 | 9 | 10 | 11 | 12 |
Check each cell for (r + c) % 2 == 0:
- (0,0): 0+0=0 ✓ → 1
- (0,1): 0+1=1 ✗
- (0,2): 0+2=2 ✓ → 3
- (0,3): 0+3=3 ✗
- (1,0): 1+0=1 ✗
- (1,1): 1+1=2 ✓ → 6
- (1,2): 1+2=3 ✗
- (1,3): 1+3=4 ✓ → 8
- (2,0): 2+0=2 ✓ → 9
- (2,1): 2+1=3 ✗
- (2,2): 2+2=4 ✓ → 11
- (2,3): 2+3=5 ✗
Sum = 1 + 3 + 6 + 8 + 9 + 11 = 38
The answer is B) 38.
Teacher Explanation
This question tests 2D array traversal and modular arithmetic. Students must identify which indices satisfy (r + c) % 2 == 0 — the "checkerboard" pattern of even-sum positions — and correctly sum the corresponding values. The total is 1 + 3 + 6 + 8 + 9 + 11 = 38.
Education Coach Advice
When tracing 2D array problems on the AP exam, draw a small grid and mark which cells satisfy the condition before summing. For a 3×4 array, there are only 12 cells to check — a quick table trace prevents arithmetic errors. Also note that (r + c) % 2 == 0 is a classic "checkerboard" pattern: it selects the "white squares" if you imagine a chessboard starting with white at (0,0).
Traps
Option A (36): A common mistake is to accidentally skip one element (e.g., forgetting m[1][3]=8 or m[2][2]=11) due to an off-by-one error in tracing the loop. Option C (42): Students might incorrectly include cells where (r + c) % 2 == 1 instead of 0, or mix up the condition. Option D (78): This is the sum of all elements in the array (1+2+...+12 = 78), which results from ignoring the if condition entirely.
Answer images
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.

