AP® Computer Science A Practice Questions
Work through AP Computer Science A practice 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 2852
Question 2853
Question 2854
Question 2855
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 2856
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 2857
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
Question 2858
After exactly 2 passes of this partial insertion sort, what is the array?
int[] arr = {8, 3, 5, 1, 4};
for (int i = 1; i < 3; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}Main Solution
Step-by-step trace of the code:
Initial array: {8, 3, 5, 1, 4}
Pass 1 (i = 1):
key = arr[1] = 3j = 0- While loop:
j >= 0(true) ANDarr[0] > key→8 > 3(true):arr[1] = arr[0]→ array becomes{8, 8, 5, 1, 4};j = -1 - While loop:
j >= 0(false) → exit arr[j + 1] = arr[0] = key = 3→ array becomes{3, 8, 5, 1, 4}
Pass 2 (i = 2):
key = arr[2] = 5j = 1- While loop:
j >= 0(true) ANDarr[1] > key→8 > 5(true):arr[2] = arr[1]→ array becomes{3, 8, 8, 1, 4};j = 0 - While loop:
j >= 0(true) ANDarr[0] > key→3 > 5(false) → exit arr[j + 1] = arr[1] = key = 5→ array becomes{3, 5, 8, 1, 4}
After exactly 2 passes, the array is {3, 5, 8, 1, 4}, which matches option B.
Teacher Explanation
This question tests students' ability to trace a partial insertion sort. The loop runs only for i = 1 and i = 2 (since i < 3), so only the first three elements get sorted among themselves. The last two elements (indices 3 and 4) remain untouched. After 2 passes, the subarray {8, 3, 5} becomes {3, 5, 8}, giving {3, 5, 8, 1, 4}.
Education Coach Advice
AP Exam Strategy: When tracing sorting algorithms, always track the exact loop bounds first. Here, i < 3 means only passes for i = 1 and i = 2 occur — only indices 0–2 are affected. Write out the array state after each pass to avoid errors. Elements beyond the loop's reach stay in their original positions.
Traps
Common traps:
- Option A ({1, 3, 5, 8, 4}): Students may think the entire array gets sorted or confuse the loop bound
i < 3with more passes, incorrectly including element1in the sorted portion. - Option C/D: Students may miscount the number of passes or misread the loop condition, thinking 4 or 5 passes occur instead of just 2.
Answer images
Question 2859
What is printed?
int[][] grid = new int[4][4];
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
if (r <= c) grid[r][c] = 1;
}
}
int sum = 0;
for (int r = 0; r < 4; r++)
for (int c = 0; c < 4; c++)
sum += grid[r][c];
System.out.println(sum);Main Solution
To determine what is printed, we need to trace through the code step by step.
Step 1: Initialize the grid.
int[][] grid = new int[4][4]; creates a 4×4 grid initialized to all zeros.
Step 2: Fill the grid.
The nested loops iterate over all (r, c) pairs where r and c each range from 0 to 3. The condition if (r <= c) sets grid[r][c] = 1 whenever the row index is less than or equal to the column index. This corresponds to the upper triangle (including the diagonal) of the matrix:
| c=0 | c=1 | c=2 | c=3 | |
|---|---|---|---|---|
| r=0 | 1 | 1 | 1 | 1 |
| r=1 | 0 | 1 | 1 | 1 |
| r=2 | 0 | 0 | 1 | 1 |
| r=3 | 0 | 0 | 0 | 1 |
Step 3: Sum all elements.
Count the number of 1s: row 0 has 4, row 1 has 3, row 2 has 2, row 3 has 1. Total = 4 + 3 + 2 + 1 = 10.
Step 4: Print.
System.out.println(sum); prints 10.
The correct answer is C) 10.
Teacher Explanation
This question tests students' ability to trace nested loops and understand 2D array indexing. The key insight is recognizing that r <= c selects the upper triangular portion of the matrix (including the main diagonal). For a 4×4 matrix, the number of cells satisfying this condition is 4+3+2+1 = 10. Students must correctly initialize the grid (all zeros by default in Java) and understand that only cells meeting the condition are set to 1.
Education Coach Advice
When tracing 2D array problems on the AP exam, draw out the grid on your scratch paper and mark each cell that satisfies the condition. Then count the marked cells. This visual approach prevents off-by-one errors and helps you quickly verify your answer. Pay close attention to whether the condition uses < or <= — this single character changes the answer significantly.
Traps
Option A (16): A student might choose this if they think every cell is set to 1, forgetting the if (r <= c) condition.
Option B (6): A student might choose this if they use the condition r < c (strict less than) instead of r <= c, missing the diagonal (which adds 4 more cells).
Option D (12): A student might miscount the 1s, perhaps including one extra row or miscounting the diagonal.
Answer images
Question 2860
What is printed?
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) { list.add(i * i); }
for (int i = 0; i < list.size() - 1; i++) {
list.set(i, list.get(i) + list.get(i + 1));
}
System.out.println(list);Main Solution
Step 1: Build the initial list.
The first loop runs for i = 0, 1, 2, 3, 4 and adds i * i:
- i=0: 00 = 0
- i=1: 11 = 1
- i=2: 22 = 4
- i=3: 33 = 9
- i=4: 4*4 = 16
List after first loop: [0, 1, 4, 9, 16]
Step 2: Run the second loop.
The second loop runs while i < list.size() - 1, i.e., i < 4, so i = 0, 1, 2, 3. Each iteration sets list[i] = list[i] + list[i+1]. Crucially, each update is immediately visible to subsequent iterations because the list is modified in place.
- i=0: list[0] = list[0] + list[1] = 0 + 1 = 1 → list:
[1, 1, 4, 9, 16] - i=1: list[1] = list[1] + list[2] = 1 + 4 = 5 → list:
[1, 5, 4, 9, 16] - i=2: list[2] = list[2] + list[3] = 4 + 9 = 13 → list:
[1, 5, 13, 9, 16] - i=3: list[3] = list[3] + list[4] = 9 + 16 = 25 → list:
[1, 5, 13, 25, 16]
The last element (index 4) is never updated because the loop stops at i < 4. It remains 16.
Final output: [1, 5, 13, 25, 16], which is option D.
Teacher Explanation
This question tests understanding of in-place ArrayList mutation during iteration. The key insight is that each list.set(i, ...) immediately changes the value at index i, so subsequent iterations use the already-updated values — not the originals. Additionally, the loop condition i < list.size() - 1 means the last element (index 4) is never written to, so it stays at 16.
Education Coach Advice
When tracing ArrayList mutations in a loop, always update your mental model of the list after each iteration — don't assume the original values persist. Also, carefully check the loop's stopping condition: i < list.size() - 1 means the last index is excluded. On the AP exam, these boundary conditions are frequently tested.
Traps
Option A [1, 5, 13, 25, 25] is the most common trap. Students who correctly trace the first four updates but then assume the last element also gets updated (perhaps thinking the loop runs one more iteration, or that the last element becomes 25 by some other logic) will choose A. The loop condition i < list.size() - 1 (i.e., i < 4) stops at i=3, so index 4 is never touched and remains 16.
Option C [1, 4, 9, 16, 16] would result if students mistakenly used the original values for all additions (i.e., treated it as if a copy were used), but only for the first element being 1 — this is inconsistent reasoning.
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.

