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: Arraylists Clear filter
Question 142
Question 143
Question 144
Question 145
Question 146
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 147
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
Question 148
What is printed?
ArrayList<String> words = new ArrayList<String>();
words.add("cat"); words.add("dog"); words.add("car");
words.add("dot"); words.add("cup");
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < words.size(); i++) {
String w = words.get(i);
if (w.substring(0, 1).equals("c")) {
result.add(w);
}
}
for (int i = result.size() - 1; i >= 0; i--) {
if (result.get(i).indexOf("a") != -1) {
result.remove(i);
}
}
System.out.println(result);Main Solution
Step 1: Build the words list.
words = ["cat", "dog", "car", "dot", "cup"]
Step 2: First loop — collect words starting with "c".
We iterate through words and check w.substring(0, 1).equals("c"):
- "cat" → starts with 'c' → added
- "dog" → starts with 'd' → skipped
- "car" → starts with 'c' → added
- "dot" → starts with 'd' → skipped
- "cup" → starts with 'c' → added
After first loop: result = ["cat", "car", "cup"]
Step 3: Second loop — iterate backwards and remove words containing "a".
We iterate from i = result.size() - 1 = 2 down to i = 0, removing any element where indexOf("a") != -1:
i = 2:result.get(2)= "cup" →"cup".indexOf("a")= -1 → NOT removedi = 1:result.get(1)= "car" →"car".indexOf("a")= 1 ≠ -1 → removed.result = ["cat", "cup"]i = 0:result.get(0)= "cat" →"cat".indexOf("a")= 1 ≠ -1 → removed.result = ["cup"]
Step 4: Print result.
System.out.println(result) prints the ArrayList's toString(), which is [cup].
The correct answer is D: [cup].
Teacher Explanation
This question tests two key skills: (1) iterating an ArrayList and filtering by string methods (substring, equals), and (2) safely removing elements from an ArrayList by iterating backwards. The backwards iteration avoids index-skipping issues. Words starting with 'c' are: "cat", "car", "cup". Then any of those containing 'a' ("cat" and "car") are removed, leaving only "cup".
Education Coach Advice
When tracing ArrayList removal loops, always simulate the index changes step by step. Backwards iteration is the safe pattern — it avoids skipping elements. For each iteration, write down the current state of the list and the current index. Also remember that indexOf returns -1 when the substring is NOT found, so != -1 means the substring IS present.
Traps
Option A [cat, car, cup]: A student might think the second loop does nothing, perhaps misreading the condition or thinking indexOf returns 0 for not found. Option B [cat, cup]: A student might think only "car" is removed (perhaps iterating forward and skipping "cat" after "car" is removed, which would actually skip "cup" — this trap is subtle). Option C [car, cup]: A student might think only "cat" is removed, perhaps confusing the condition or only checking the first character. The key insight is that BOTH "cat" and "car" contain the letter 'a', so both are removed, leaving only "cup".
Answer images
Question 149
What is printed?
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++) {
list.add(i * i);
}
// list = [1, 4, 9, 16, 25, 36]
for (int i = list.size() - 1; i >= 1; i -= 2) {
list.set(i, list.get(i) - list.get(i - 1));
}
System.out.println(list);Main Solution
The list is first populated with squares of 1 through 6:
list = [1, 4, 9, 16, 25, 36] (indices 0–5)
The second loop runs with i starting at list.size() - 1 = 5, decrementing by 2 each iteration, and stopping when i < 1. So i takes values: 5, 3, 1.
For each iteration, the operation is: list.set(i, list.get(i) - list.get(i - 1))
- i = 5: list.set(5, list.get(5) - list.get(4)) = list.set(5, 36 - 25) = list.set(5, 11)
list = [1, 4, 9, 16, 25, 11] - i = 3: list.set(3, list.get(3) - list.get(2)) = list.set(3, 16 - 9) = list.set(3, 7)
list = [1, 4, 9, 7, 25, 11] - i = 1: list.set(1, list.get(1) - list.get(0)) = list.set(1, 4 - 1) = list.set(1, 3)
list = [1, 3, 9, 7, 25, 11]
Final list: [1, 3, 9, 7, 25, 11], which matches option A.
Teacher Explanation
This question tests students' ability to trace through an ArrayList manipulation loop that iterates backwards with a step of -2, modifying only odd-indexed elements. The key insight is that the loop modifies indices 5, 3, and 1 (in that order), and each modification uses the already-updated list values — but since we go from high to low indices, the values at i-1 (even indices) are never modified and remain as the original squares.
Education Coach Advice
When tracing loops that modify a list in-place, always track the current state of the list after each iteration. Write out the list after each set() call. For loops that decrement by 2, carefully list all values of i before starting the trace: here 5 → 3 → 1 → (0 fails condition i >= 1, so stop). Don't forget the last iteration!
Traps
Option B [1, 4, 9, 7, 25, 11] is the most common trap: students may miss the final iteration where i = 1 and forget to update index 1 (changing 4 to 3). They correctly compute indices 5 and 3 but stop the loop trace early.
Option D [1, 3, 5, 7, 11, 11] is another trap for students who think the loop modifies all elements or uses cumulative differences incorrectly.
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.

