AP CSA Practice Test: ArrayList

AP CSA Practice Test: ArrayList

Unit 4: Data Collections | 10 Questions | AP Computer Science A

Question 1
What is the output?
ArrayList list = new ArrayList<>();
list.add(5);
list.add(10);
list.add(15);
list.remove(1);
System.out.println(list);
A[5, 15]
B[5, 10]
C[10, 15]
D[5, 10, 15]

Explanation: remove(1) removes the element at INDEX 1, which is 10. The remaining elements shift left: [5, 15].

Common Mistake: Thinking remove(1) removes the value 1. With Integer lists, remove(int) is index-based.

Question 2
What happens when this code runs?
ArrayList list = new ArrayList<>();
list.add(1); list.add(2); list.add(3);
for (int i = 0; i < list.size(); i++) {
    list.remove(i);
}
System.out.println(list);
A[]
B[2]
C[2, 3]
DIndexOutOfBoundsException

Explanation: i=0: remove index 0, list=[2,3], size=2. i=1: remove index 1, list=[2], size=1. i=2: 2 < 1 is false, loop ends. Result: [2]. Elements shift left after each removal, causing skips.

Common Mistake: Expecting an empty list. Forward traversal + removal skips elements due to shifting.

Question 3
Which declaration causes a compile error?
AArrayList list = new ArrayList<>();
BArrayList list = new ArrayList<>();
CArrayList list = new ArrayList<>();
DArrayList list = new ArrayList<>();

Explanation: ArrayList cannot hold primitive types. int is a primitive. You must use the wrapper class Integer instead. The other options use valid reference types.

Common Mistake: Forgetting that ArrayList requires wrapper classes: Integer, Double, Boolean.

Question 4
What is the output?
ArrayList list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
list.set(3, "Z");
System.out.println(list);
A[A, X, B, Z]
B[A, X, B, C]
C[X, A, B, Z]
D[A, X, Z, C]

Explanation: After adds: [A, B, C]. add(1, "X") inserts at index 1, shifting right: [A, X, B, C]. set(3, "Z") replaces index 3: [A, X, B, Z].

Common Mistake: Confusing add(index, value) which inserts/shifts vs set(index, value) which replaces.

Question 5
What is the correct way to remove all even numbers from an ArrayList?
AUse a for-each loop and call remove()
BUse a forward for loop and call remove(i)
CUse a backward for loop and call remove(i)
DUse list.removeAll() with no parameters

Explanation: Backward traversal prevents index shifting problems. For-each loop throws ConcurrentModificationException. Forward loop skips elements after removal. removeAll() requires a collection argument.

Common Mistake: Using for-each with remove(). This throws ConcurrentModificationException at runtime.

Question 6
What is the output?
ArrayList list = new ArrayList<>();
list.add(10); list.add(20); list.add(30);
int sum = 0;
for (int n : list) {
    sum += n;
}
System.out.println(sum);
A60
B30
C0
DCompile error

Explanation: Enhanced for loop iterates over each Integer in the list. Autoboxing converts each Integer to int. 10 + 20 + 30 = 60.

Common Mistake: Thinking enhanced for loop doesn't work with ArrayList. It works with any Iterable.

Question 7
What is the output?
ArrayList list = new ArrayList<>();
list.add("dog");
list.add("cat");
list.add("dog");
System.out.println(list.indexOf("dog"));
System.out.println(list.size());
A0 then 3
B2 then 3
C0 then 2
D-1 then 3

Explanation: indexOf("dog") returns the FIRST occurrence, which is index 0. size() returns 3 (three elements total). indexOf does not remove duplicates.

Common Mistake: Expecting indexOf to return the last occurrence. It always returns the first.

Question 8
What is the output?
ArrayList nums = new ArrayList<>();
nums.add(5);
nums.add(3);
nums.add(8);
Integer removed = nums.remove(0);
System.out.println(removed + " " + nums.get(0));
A5 3
B5 5
C0 3
D5 8

Explanation: remove(0) removes and returns the element at index 0 (which is 5). The remaining elements shift left: [3, 8]. nums.get(0) is now 3.

Common Mistake: Forgetting that remove() returns the removed element.

Question 9
Consider the following. What is the size of result?
ArrayList list = new ArrayList<>();
for (int i = 0; i < 10; i++) { list.add(i); }
ArrayList result = new ArrayList<>();
for (int n : list) {
    if (n % 3 == 0) {
        result.add(n);
    }
}
A3
B4
C10
D0

Explanation: list = [0,1,2,3,4,5,6,7,8,9]. Numbers divisible by 3: 0, 3, 6, 9. That is 4 elements.

Common Mistake: Forgetting that 0 % 3 == 0 is true. Zero is divisible by every number.

Question 10
Which is true about ArrayList vs arrays?

I. ArrayLists can grow and shrink dynamically
II. Arrays can store primitives, ArrayLists cannot
III. arr.length and list.size() both use parentheses
AI and II only
BI and III only
CI, II, and III
DII and III only

Explanation: I: true, ArrayLists resize automatically. II: true, ArrayLists require wrapper classes. III: false, arr.length has NO parentheses (it is a field), while list.size() has parentheses (it is a method).

Common Mistake: Confusing .length (array field, no parens) with .size() (ArrayList method, has parens).

0% 0/10

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]