AP Computer Science A Practice Quiz: ArrayList Methods
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 10 questions to check your progress.
Question 1 of 10
All Questions (10)
A) 0
B) 2
C) 3
D) 4
Correct Answer: C
The ArrayList is initialized with a size of 0. The add(E obj) method is called three times, adding one element to the list each time. Therefore, the final size of the list is 3.
A) 10
B) 20
C) 30
D) An IndexOutOfBoundsException is thrown.
Correct Answer: B
ArrayLists use zero-based indexing. The element 10 is at index 0, the element 20 is at index 1, and the element 30 is at index 2. The method call get(1) retrieves the element at index 1, which is 20.
A) "green"
B) "blue"
C) "red"
D) "yellow"
Correct Answer: D
The list is initially ["red", "green", "blue"]. The call colors.set(1, "yellow") replaces the element at index 1 ("green") with "yellow". The list becomes ["red", "yellow", "blue"]. Therefore, colors.get(1) returns "yellow".
A) ["A", "B", "C", "D"]
B) ["A", "B", "D", "C"]
C) ["A", "C", "B", "D"]
D) ["A", "B", "C"]
Correct Answer: A
The list starts as ["A", "B", "D"]. The call letters.add(2, "C") inserts the string "C" at index 2. The element that was originally at index 2 ("D") and all subsequent elements are shifted one position to the right. The final list is ["A", "B", "C", "D"].
A) 88
B) 72
C) 100
D) 95
Correct Answer: B
The list is initially [95, 88, 72, 100]. The call scores.remove(1) removes the element at index 1, which is 88. All elements after the removed element are shifted to the left. The list becomes [95, 72, 100]. The new element at index 1 is 72, which is what scores.get(1) returns.
A) "pen"
B) "marker"
C) "pencil"
D) "crayon"
Correct Answer: B
Let's trace the state of the list: 1. After adds: ["pen", "pencil", "eraser"] 2. After items.add(1, "marker"): ["pen", "marker", "pencil", "eraser"] 3. After items.set(2, "crayon"): ["pen", "marker", "crayon", "eraser"] 4. After items.remove(0): ["marker", "crayon", "eraser"] Finally, items.get(0) retrieves the element at index 0, which is now "marker".
A) An ArrayList has a fixed size that cannot be changed after creation.
B) An ArrayList is mutable in size and holds references to objects.
C) The non-generic version, ArrayList, is preferred over ArrayList<E>.
D) The get() method is used to change the value of an element at a specific index.
Correct Answer: B
The provided content explicitly states that 'An ArrayList object is mutable in size and contains object references.' This means its size can change, and it stores references, not primitive types directly. Option A is incorrect because the size is mutable. Option C is incorrect because ArrayList<E> is preferred. Option D is incorrect; the set() method changes an element, while get() retrieves it.
A) "Song A Song B"
B) "Song X Song B"
C) "Song A Song C"
D) "Song X Song C"
Correct Answer: A
1. The list starts as ["Song A", "Song B", "Song C"]. 2. `playlist.set(0, "Song X")` replaces "Song A" with "Song X". The `set` method returns the element that was replaced, so `replacedSong` becomes "Song A". The list is now ["Song X", "Song B", "Song C"]. 3. `playlist.remove(1)` removes the element at index 1, which is now "Song B". The `remove` method returns the element that was removed, so `removedSong` becomes "Song B". 4. The code prints the values of `replacedSong` and `removedSong`, resulting in "Song A Song B".
A) 100
B) 200
C) 300
D) 400
Correct Answer: B
The `set(int index, E obj)` method replaces the element at the specified index with the new object. It also returns the element that was previously at that position. In this code, the element at index 1 is 200. When `data.set(1, 400)` is called, 200 is replaced by 400, and the value 200 is returned and stored in the `oldValue` variable, which is then printed.
A) "3 Y"
B) "4 X"
C) "4 Y"
D) "3 Z"
Correct Answer: C
Let's trace the list's state: 1. After `list.add("X")` and `list.add("Y")`: ["X", "Y"] 2. After `list.add(0, "W")`: ["W", "X", "Y"] 3. After `list.add("Z")`: ["W", "X", "Y", "Z"] The final size of the list is 4. The element at index 2 is "Y". Therefore, the code prints "4 Y".