ArrayList Practice Questions

AP (Advanced Placement) · AP Computer Science A · 151 free MCQs with instant results and detailed explanations.

151
Total
47
Easy
73
Medium
31
Hard

Start Practicing ArrayList

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from ArrayList

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What is the primary purpose of the ArrayList class in Java?
A. To store a fixed-size collection of elements.
B. To create a dynamic array that can grow in size.
C. To implement a stack data structure.
D. To manipulate strings more efficiently.
Show Answer & Explanation
Correct Answer: B
The ArrayList class is designed to create a dynamic array that can grow and shrink as elements are added or removed, making it more flexible compared to traditional arrays.
Q2
Easy
Given the following code snippet, what will be the output of System.out.println(list.get(1)); when list is an ArrayList containing [10, 20, 30]?
A. 10
B. 20
C. 30
D. IndexOutOfBoundsException
Show Answer & Explanation
Correct Answer: B
ArrayList uses 0-based indexing, so list.get(1) retrieves the second element, which is 20 in this case.
Q3
Easy
Which method would you use to add an element at a specific index in an ArrayList?
A. addElement()
B. insert()
C. add(index, element)
D. put(index, element)
Show Answer & Explanation
Correct Answer: C
The correct method to add an element at a specific index in an ArrayList is add(index, element), which allows for precise control over the position of the new element.
Q4
Medium
Which method of the ArrayList class is used to add an element at a specific index?
A. add(int index, E element)
B. insert(int index, E element)
C. put(int index, E element)
D. place(int index, E element)
Show Answer & Explanation
Correct Answer: A
The correct method to add an element at a specific index in an ArrayList is 'add(int index, E element)'. This method is specifically designed for inserting elements at defined positions.
Q5
Medium
What will be the output of the following code? ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); System.out.println(list.get(1));
A. A
B. B
C. C
D. IndexOutOfBoundsException
Show Answer & Explanation
Correct Answer: B
In the provided code, 'list.get(1)' retrieves the element at index 1 of the ArrayList, which is "B". ArrayLists are zero-indexed, meaning the first element is at index 0.
Q6
Medium
Given an ArrayList called 'numbers' containing the elements {10, 20, 30}, what will 'numbers.size()' return?
A. 2
B. 3
C. 4
D. 1
Show Answer & Explanation
Correct Answer: B
'numbers.size()' returns the number of elements in the ArrayList. Since it contains three elements (10, 20, and 30), the correct output is 3.
Q7
Medium
What is the result of calling 'list.remove(0)' on an ArrayList containing {"first", "second", "third"}?
A. first
B. second
C. third
D. null
Show Answer & Explanation
Correct Answer: A
Calling 'list.remove(0)' removes the element at index 0, which is "first". The method returns the element that was removed.
Q8
Hard
Consider the following code snippet that manipulates an ArrayList of Integers: ```java ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40); numbers.remove(2); numbers.add(1, 25); ``` What will be the final contents of the 'numbers' ArrayList?
A. [10, 25, 20, 40]
B. [25, 10, 20, 40]
C. [10, 20, 25, 40]
D. [10, 20, 40, 25]
Show Answer & Explanation
Correct Answer: A
The original list was [10, 20, 30, 40]. After removing the element at index 2 (which is 30), the list becomes [10, 20, 40]. Then, 25 is added at index 1, resulting in [10, 25, 20, 40]. Hence, the correct answer is [10, 25, 20, 40].
Q9
Hard
Given the following code: ```java ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); fruits.add("Cherry"); int index = fruits.indexOf("Banana"); fruits.set(index, "Blueberry"); fruits.remove("Apple"); ``` What is the final ArrayList contents?
A. [Banana, Mango, Cherry]
B. [Blueberry, Mango, Cherry]
C. [Mango, Cherry, Blueberry]
D. [Apple, Blueberry, Mango, Cherry]
Show Answer & Explanation
Correct Answer: B
After adding the fruits, the list is [Apple, Banana, Mango, Cherry]. The index of 'Banana' is 1. Setting 'Blueberry' at this index gives us [Apple, Blueberry, Mango, Cherry]. Removing 'Apple' results in [Blueberry, Mango, Cherry]. Thus, the final list is [Blueberry, Mango, Cherry].
Q10
Hard
What will be the output of the following code snippet? ```java import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.remove(1); System.out.println(numbers.get(1)); } } ```
A. 10
B. 20
C. 30
D. IndexOutOfBoundsException
Show Answer & Explanation
Correct Answer: C
The code adds three integers to the ArrayList: 10, 20, and 30. The `remove(1)` call removes the element at index 1 (20). After this, the element at index 1 is now 30, so `numbers.get(1)` returns 30.

Showing 10 of 151 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 151 Questions →

ArrayList โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 151 practice MCQs for the chapter ArrayList in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 47 easy, 73 medium, 31 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.