AP CSA Practice Test: ArrayList
AP CSA Practice Test: ArrayList
Unit 4: Data Collections | 10 Questions | AP Computer Science A
ArrayListlist = new ArrayList<>(); list.add(5); list.add(10); list.add(15); list.remove(1); System.out.println(list);
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.
ArrayListlist = 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);
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.
ArrayList list = new ArrayList<>();
ArrayList list = new ArrayList<>();
ArrayList list = new ArrayList<>();
ArrayList 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.
ArrayListlist = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add(1, "X"); list.set(3, "Z"); System.out.println(list);
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.
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.
ArrayListlist = new ArrayList<>(); list.add(10); list.add(20); list.add(30); int sum = 0; for (int n : list) { sum += n; } System.out.println(sum);
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.
ArrayListlist = new ArrayList<>(); list.add("dog"); list.add("cat"); list.add("dog"); System.out.println(list.indexOf("dog")); System.out.println(list.size());
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.
ArrayListnums = new ArrayList<>(); nums.add(5); nums.add(3); nums.add(8); Integer removed = nums.remove(0); System.out.println(removed + " " + nums.get(0));
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.
result?ArrayListlist = 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); } }
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.
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 parenthesesExplanation: 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).
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]