AP Computer Science A ArrayList — Worked Answer Explanations
Unit 7 · 12 questions explained
Below is a complete answer key for our AP Computer Science A ArrayList practice questions. For each question you'll find the correct choice, a full written explanation of how to get there, and — for every wrong answer — a short note on exactly why it's tempting and where it goes wrong. Reading these straight through is one of the fastest ways to find the gaps in a unit before exam day.
Prefer to test yourself first? Take the timed ArrayList practice test and come back here to review, or head back to the ArrayList unit overview.
- Question 1 · Easy
What is the output of the following code?
import java.util.ArrayList; ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); System.out.println(list.size()); System.out.println(list.get(1));- A3\n20Correct
- B3\n10Why not B:
get(1)returns the element at index 1 (zero-indexed), which is 20, not 10 (which is at index 0). - C2\n20Why not C:
size()returns the number of elements (3), not the last valid index (2). - D3\n30Why not D:
get(1)is index 1, which is 20. 30 is at index 2.
Explanationsize()returns the number of elements: 3.get(1)retrieves the element at index 1 (0-indexed): 20. Note thatsize()is always 1 more than the last valid index.Key takeaway`ArrayList.size()` is one more than the last valid index. Elements are 0-indexed like arrays.
- A
- Question 2 · Easy
What is the state of
listafter the following code executes?ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add(1, "X");- A[A, B, X, C]Why not A:
add(1, "X")inserts "X" AT index 1, shifting "B" and "C" right. Result is [A, X, B, C]. - B[A, X, B, C]Correct
- C[X, A, B, C]Why not C:
add(1, "X")inserts at index 1 (second position), not index 0. - D[A, B, C, X]Why not D:
add(index, value)inserts at the specified position. The no-indexaddappends to the end, butadd(1, "X")inserts at index 1.
Explanationadd(int index, E element)inserts at the given position, shifting everything from that index onward to the right. After three adds: [A, B, C]. Afteradd(1, "X"): [A, X, B, C].Key takeaway`add(index, value)` inserts at the given index; all elements at that index and beyond shift right by one.
- A
- Question 3 · Easy
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>(); list.add(5); list.add(10); list.add(15); list.remove(1); System.out.println(list);- A[5, 15]Correct
- B[5, 10]Why not B:
remove(1)removes the element at INDEX 1 (which is 10), not the last element. - C[10, 15]Why not C:
remove(1)removes the element at index 1 (10). The remaining elements shift left: [5, 15]. - D[5, 10, 15]Why not D:
removemodifies the list. One element is removed; the list has 2 elements, not 3.
Explanationremove(int index)removes the element at that index. Index 1 holds value 10. After removal: [5, 15]. The element at index 2 (15) shifts left to index 1.Key takeaway`remove(int index)` removes by position; `remove(Object)` removes by value. For Integer lists, `remove(1)` is by index.
- A
- Question 4 · Easy
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= 5; i++) { list.add(i); } for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); }- A1 2 3 4 5Correct
- B0 1 2 3 4Why not B: The first loop adds values 1 through 5, not the indices 0 through 4.
- C1 2 3 4Why not C: The condition
i < list.size()(i < 5) iterates i = 0,1,2,3,4, accessing all 5 elements. - D2 3 4 5 6Why not D: The first loop adds
i(1–5), noti+1. The values added are exactly 1, 2, 3, 4, 5.
ExplanationThe first loop adds integers 1 through 5 to the list. The second loop accesses indices 0 through 4 with
list.get(i), printing elements 1, 2, 3, 4, 5 in order.Key takeawayIndex-based ArrayList traversal: `for (int i = 0; i < list.size(); i++)` with `list.get(i)` to access elements.
- A
- Question 5 · Easy
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 0) { list.remove(i); } } System.out.println(list);- A[2, 4]Why not A: Reversed the condition — this code removes evens, not odds.
- B[1, 3, 4]Why not B: Missed that after 2 is removed, 4 shifts to index 1 but i becomes 2; then list.get(2) is 4 and also gets removed.
- C[1, 3]Correct
- D[1, 2, 3]Why not D: Confused which element gets skipped — when an element is removed at index i and i increments, the new element at i is what gets re-checked.
ExplanationTrace: list=[1,2,3,4]. i=0: 1 is odd (keep). i=1: 2 is even (remove) → list=[1,3,4]; i++ → i=2. i=2: list.get(2)=4 (even, remove) → list=[1,3]; i++ → i=3. 3 >= size(2), loop ends. Result: [1, 3]. Note: 3 was skipped because after removing 2, index 1 now holds 3, but i jumped to 2.
Key takeawayRemoving elements during forward traversal skips elements. Fix: iterate backward, or use an iterator, or decrement i after removal.
- A
- Question 6 · Easy
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>(); list.add(100); list.add(200); list.set(0, 999); System.out.println(list.get(0)); System.out.println(list.size());- A999\n2Correct
- B999\n3Why not B:
setreplaces the element at the given index — it does NOT add a new element. Size remains 2. - C100\n2Why not C:
set(0, 999)replaces the element at index 0 (previously 100) with 999. get(0) now returns 999. - D100\n3Why not D:
setreplaces, not adds. The element at index 0 is replaced, not a new element added.
Explanationset(index, value)replaces the element at the given index with the new value. It does NOT change the size of the list. Afterset(0, 999), index 0 holds 999.size()is still 2.Key takeaway`set(index, value)` replaces an existing element. `add(value)` appends. `add(index, value)` inserts. Only add-variants increase size.
- A
- Question 7 · Easy
What is the output of the following code?
ArrayList<String> list = new ArrayList<>(); list.add("dog"); list.add("cat"); list.add("dog"); list.add("bird"); int count = 0; for (String s : list) { if (s.equals("dog")) count++; } System.out.println(count);- A1Why not A: "dog" appears twice in the list (at indices 0 and 2). The enhanced for loop visits all elements.
- B2Correct
- C3Why not C: Only "dog" entries are counted. "cat" and "bird" do not match. There are 2 "dog" entries.
- D4Why not D: count only increments when s.equals("dog"). The list has 4 elements total but only 2 are "dog".
ExplanationThe enhanced for loop visits all elements. Using
.equals()for String comparison: "dog" (match, count=1), "cat" (no), "dog" (match, count=2), "bird" (no). count = 2.Key takeawayUse `.equals()` (not `==`) to compare String values in an ArrayList. The enhanced for loop visits every element.
- A
- Question 8 · Easy
Which of the following correctly removes all elements equal to 0 from an ArrayList without skipping elements?
ArrayList<Integer> list = new ArrayList<>(); // list contains: [0, 5, 0, 3, 0]- A
java\nfor (int i = 0; i < list.size(); i++) {\n if (list.get(i) == 0) list.remove(i);\n}Why not A: Forward traversal with removal skips elements. After removing at index 0, the next element shifts to index 0, but i jumps to 1. - B
java\nfor (int i = list.size() - 1; i >= 0; i--) {\n if (list.get(i) == 0) list.remove(i);\n}Correct - C
java\nfor (int val : list) {\n if (val == 0) list.remove(val);\n}Why not C: Modifying a list while using an enhanced for loop throws ConcurrentModificationException at runtime. - D
java\nlist.remove(0);Why not D:list.remove(0)removes only the element at index 0, not all zeros. And calling it once only removes one element.
ExplanationBackward traversal (B) is the safe way to remove elements by index: removing an element only shifts elements AFTER it, which have already been processed. Option C throws
ConcurrentModificationException. Option A skips elements.Key takeawayTo safely remove elements during traversal, iterate BACKWARD (from size-1 to 0). Or use an Iterator with remove().
- A
- Question 9 · Easy
What is the output of the following code?
ArrayList<Double> list = new ArrayList<>(); list.add(1.5); list.add(2.5); list.add(3.5); double sum = 0; for (double d : list) { sum += d; } System.out.println(sum);- A7.5Correct
- B6.0Why not B: 1.5+2.5+3.5 = 7.5, not 6.0. All three values are added.
- CCompile error: cannot use double in enhanced for with ArrayListWhy not C: Java auto-unboxes
Doubleobjects todoubleprimitives in the enhanced for loop. This is valid. - D7.0Why not D: 1.5+2.5+3.5 = 7.5 exactly. No truncation occurs here.
ExplanationArrayList<Double>storesDoubleobjects. The enhanced for loop withdouble dautomatically unboxes eachDoubleto adouble. Sum = 1.5 + 2.5 + 3.5 = 7.5.Key takeawayJava auto-unboxes wrapper types (Double, Integer) to primitives when assigned to a primitive variable in an enhanced for loop.
- A
- Question 10 · Medium
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { list.add(i * 2); } list.remove(Integer.valueOf(4)); System.out.println(list);- A[0, 2, 6, 8]Correct
- B[0, 2, 4, 6, 8]Why not B:
remove(Integer.valueOf(4))calls the Object-based remove, which removes the FIRST occurrence of value 4. List becomes [0, 2, 6, 8]. - C[0, 2, 4, 8]Why not C:
remove(Integer.valueOf(4))removes the element with VALUE 4 (at index 2), not the element at index 4. Element at index 4 is 8. - D[0, 2, 4, 6]Why not D:
remove(Integer.valueOf(4))removes value 4, not the last element. 8 remains in the list.
ExplanationThe list after the loop: [0, 2, 4, 6, 8].
remove(Integer.valueOf(4))calls theremove(Object)overload, which removes the first element EQUAL TO 4 (value 4 at index 2). Result: [0, 2, 6, 8].Key takeawayFor `ArrayList<Integer>`, `remove(int)` removes by index; `remove(Integer.valueOf(n))` removes by value. Know which overload is called.
- A
- Question 11 · Medium
What is the output of the following code?
ArrayList<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("cherry"); String result = ""; for (int i = list.size() - 1; i >= 0; i--) { result += list.get(i).charAt(0); } System.out.println(result);- AabcWhy not A: The loop iterates backward (from index 2 to 0). First charAt(0) = 'c' (cherry), then 'b' (banana), then 'a' (apple).
- BcbaCorrect
- Ccherry banana appleWhy not C:
charAt(0)returns only the first character of each word, not the whole word. - DcabWhy not D: The loop goes backward: index 2 (cherry→'c'), index 1 (banana→'b'), index 0 (apple→'a'). Order is c, b, a.
ExplanationBackward traversal: i=2 → list.get(2)="cherry" → charAt(0)='c'. i=1 → "banana" → 'b'. i=0 → "apple" → 'a'. result = "cba".
Key takeawayBackward traversal processes elements from last to first. Combine with charAt(0) to extract first characters in reverse order.
- A
- Question 12 · Medium
What is the output of the following code?
ArrayList<Integer> a = new ArrayList<>(); a.add(1); a.add(2); a.add(3); ArrayList<Integer> b = new ArrayList<>(a); b.add(4); System.out.println(a.size()); System.out.println(b.size());- A4\n4Why not A:
new ArrayList<>(a)creates a NEW ArrayList copying a's elements. Adding to b does NOT affect a. a.size() = 3. - B3\n4Correct
- C3\n3Why not C: b starts with 3 elements (copied from a), then b.add(4) adds one more. b.size() = 4.
- D4\n3Why not D:
new ArrayList<>(a)creates an independent copy. Modifying b does not affect a.
Explanationnew ArrayList<>(a)creates a copy of the list — a new ArrayList containing the same elements, but it's a separate object. Modifications tobdon't affecta. a has 3 elements; b gets a copy of those 3 plus adds 4, so b has 4.Key takeaway`new ArrayList<>(otherList)` creates an independent copy. Unlike `b = a` (same reference), modifications to the copy don't affect the original.
- A