PrepGo

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

Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("apple"); words.add("banana"); words.add("cherry"); What is the value of words.size() after this code has executed?

All Questions (10)

Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("apple"); words.add("banana"); words.add("cherry"); What is the value of words.size() after this code has executed?

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.

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(10); nums.add(20); nums.add(30); What is the value of nums.get(1)?

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.

What is printed as a result of executing the following code segment? ArrayList<String> colors = new ArrayList<String>(); colors.add("red"); colors.add("green"); colors.add("blue"); colors.set(1, "yellow"); System.out.println(colors.get(1));

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".

Consider the following code segment. ArrayList<String> letters = new ArrayList<String>(); letters.add("A"); letters.add("B"); letters.add("D"); letters.add(2, "C"); What is the state of the letters list after the code executes?

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"].

What is printed as a result of executing the following code segment? ArrayList<Integer> scores = new ArrayList<Integer>(); scores.add(95); scores.add(88); scores.add(72); scores.add(100); scores.remove(1); System.out.println(scores.get(1));

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.

What is the output of the following code segment? ArrayList<String> items = new ArrayList<String>(); items.add("pen"); items.add("pencil"); items.add("eraser"); items.add(1, "marker"); items.set(2, "crayon"); items.remove(0); System.out.println(items.get(0));

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".

Based on the provided information, which statement accurately describes a fundamental property of an ArrayList?

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.

What is printed by the following code segment? ArrayList<String> playlist = new ArrayList<String>(); playlist.add("Song A"); playlist.add("Song B"); playlist.add("Song C"); String replacedSong = playlist.set(0, "Song X"); String removedSong = playlist.remove(1); System.out.println(replacedSong + " " + removedSong);

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".

What is the output of the following code? ArrayList<Integer> data = new ArrayList<Integer>(); data.add(100); data.add(200); data.add(300); Integer oldValue = data.set(1, 400); System.out.println(oldValue);

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.

What is printed by the following code segment? ArrayList<String> list = new ArrayList<String>(); list.add("X"); list.add("Y"); list.add(0, "W"); list.add("Z"); System.out.println(list.size() + " " + list.get(2));

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".