AP CSA Unit 7: ArrayList
283 multiple-choice questions on AP Computer Science A Unit 7, ArrayList, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 37 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.
Take the practice test · Flashcards · All study tools
What it covers
- 7.1 ArrayList basics
- 7.2 Traversing ArrayLists
- 7.3 Arrays versus ArrayLists
Key terms
7.1 ArrayList basics
- add(index, obj)
- Inserts obj at index, shifting later elements right
- ArrayList
- A resizable list of objects from java.util
- Empty ArrayList
- A list that has been constructed but does not yet contain any elements, so size is 0
- Generic type argument
- The object type written in angle brackets when an ArrayList is declared, such as String in ArrayList<String>
- Indexed insertion
- The call add(index, obj) that places a new element at a chosen index
- IndexOutOfBoundsException
- The run-time error thrown when get, set, or remove uses an index outside 0 through size minus 1, or when add uses an index outside 0 through size
- Insertion shift
- The rightward movement of later elements that happens when a value is inserted at an existing index
- Last valid list index
- The index of the final element, equal to size() minus 1
- Object append
- The call add(obj) that places a new element at the end of the list and returns true
- Removal shift
- The leftward movement of later elements that happens after an element is removed by index
- remove(index)
- Removes the element at index, shifts later elements left, and returns the removed element
- Returned prior element
- The value that set returns: the element that used to occupy the index being replaced
- set(index, obj)
- Replaces the element at index and returns the old element
- size()
- The number of elements in an ArrayList
- Type argument
- The class name supplied in angle brackets so the list can hold only that kind of object (or a subclass)
7.2 Traversing ArrayLists
- Backward removal
- A loop that starts at size() minus 1 and moves toward 0, removing matching elements as it goes
- Bounded list index
- An index that stays inside 0 through size() minus 1 for get, set, and remove
- ConcurrentModificationException
- Error from changing a list's size inside an enhanced for loop
- Enhanced-for list loop
- A loop of the form for (Type item : list) that visits each element without an index variable
- Enhanced-for removal
- The illegal pattern of calling add or remove inside for (Type item : list), which changes the list's size during the walk
- Forward removal with decrement
- A forward index loop that subtracts one from the index after a removal, so the next increment revisits the shifted element
- Indexed list traversal
- A loop that visits ArrayList elements by counting an index and calling get or set
- Loop decrement after remove
- The adjustment i-- used in a forward loop right after list.remove(i), so the element that shifted left is checked on the next pass
- Removal during traversal
- Deleting elements from a list in the same loop that is walking through it
- Size-changing traversal
- A walk through a list that also adds or removes elements, which changes later indexes and can skip or double-visit values
- Skip-after-remove
- The bug in a forward index loop that increments after a removal, so the element that just shifted into that index is never examined
7.3 Arrays versus ArrayLists
- Autoboxed element
- A primitive value that Java automatically wraps, such as turning the int 17 into an Integer when it is added to an ArrayList<Integer>
- Fixed-length array
- An array object whose number of slots is chosen at creation and cannot change later
- Index removal
- The ArrayList method remove(int) that deletes the element at a numeric index and returns that element
- Integer remove overload
- The rule that list.remove(n) on an ArrayList<Integer> uses the index n, because n is an int, not a boxed Integer
- Privacy of personal data
- The obligation to consider how a program collects, stores, and shares information about people, including data kept in lists
- Resizable list
- A collection such as ArrayList whose number of elements can grow or shrink after the object is created
- Shared list reference
- Two variables that refer to the same ArrayList object, so a change through one name is visible through the other
- Unboxing from a list
- The automatic conversion of a wrapper object from a list, such as Integer, back to a primitive when it is used in arithmetic
- Value removal
- The ArrayList method remove(Object) that deletes the first element equal to the given object and returns whether it was found
- Wrapper class
- A class such as Integer or Double that holds a primitive value as an object
- Wrapper list
- An ArrayList whose type argument is a wrapper class such as Integer or Double, so it can store boxed primitive values
Sample practice questions
Which statement creates an empty ArrayList of Strings?
- ArrayList list = new String[];
- ArrayList<String> list = new ArrayList<String>();
- ArrayList<String> list = new ArrayList<String>[10];
- String<ArrayList> list = new ArrayList();
Answer: B. ArrayList<String> list = new ArrayList<String>(); Generic type parameters go in angle brackets.
Which loop correctly prints every element of ArrayList<String> list?
- for (int i = 0; i < list.length; i++) System.out.print(list[i]);
- for (int i = 0; i <= list.size(); i++) System.out.print(list.get(i));
- for (int i = 0; i < list.size(); i++) System.out.print(list.get(i));
- for (int i = 1; i < list.size(); i++) System.out.print(list.get(i));
Answer: C. for (int i = 0; i < list.size(); i++) System.out.print(list.get(i)); Use size() and get(i) with indices 0 through size() − 1.
Which is an advantage of an ArrayList over an array?
- An ArrayList can hold primitive types such as int directly, without wrappers
- An ArrayList uses square brackets for indexing, which is faster than get
- An ArrayList has a fixed size, which prevents index errors at run time
- An ArrayList can grow and shrink as elements are added and removed
Answer: D. An ArrayList can grow and shrink as elements are added and removed Arrays have a fixed length; ArrayLists resize automatically.
Which statement is NOT valid?
- ArrayList<int> nums = new ArrayList<int>();
- ArrayList<Integer> nums = new ArrayList<Integer>();
- ArrayList<Double> vals = new ArrayList<Double>();
- ArrayList<String> words = new ArrayList<String>();
Answer: A. ArrayList<int> nums = new ArrayList<int>(); ArrayLists hold objects, so use the wrapper class Integer instead of int.
Removing elements from an ArrayList while traversing it with an enhanced for loop
- causes a ConcurrentModificationException
- works correctly and removes the elements
- removes every element in the list
- causes a compile-time error in the loop
Answer: A. causes a ConcurrentModificationException Changing the size of a list during an enhanced for loop is not allowed.
For an ArrayList<Integer> nums, the expression nums.get(0) + 1
- causes a compile-time error
- unboxes the Integer to an int and adds 1
- concatenates "1"
- returns an Integer object equal to null
Answer: B. unboxes the Integer to an int and adds 1 Java unboxes Integer values automatically in arithmetic.
After list.add("a"); list.add("b"); list.add(1, "c");, the list is
- [a, b, c]
- [c, a, b]
- [a, c, b]
- [a, c]
Answer: C. [a, c, b] add(1, "c") inserts at index 1 and shifts b right.
This code is meant to remove all even numbers from list: for (int i = 0; i < list.size(); i++) if (list.get(i) % 2 == 0) list.remove(i); What is wrong?
- It removes the odd numbers instead, because % 2 == 0 tests for odd
- It causes an infinite loop, because list.size() never changes
- After a removal, the next element shifts into index i and is skipped
- Nothing is wrong; every even number is removed from the list
Answer: C. After a removal, the next element shifts into index i and is skipped For [2, 4, 5], it removes 2, then 4 shifts to index 0 but i moves to 1. Traverse backward or don't increment after removing.
Calling list.remove(Integer.valueOf(7)) on an ArrayList<Integer>
- removes the first element equal to 7
- removes the element at index 7
- removes every element equal to 7
- causes a compile-time error
Answer: A. removes the first element equal to 7 remove(Object) removes by value; remove(int) removes by index.
For an ArrayList list containing [10, 20, 30], what does list.set(1, 25) return?
- 25
- true
- 20
- Nothing; it is void
Answer: C. 20 set replaces the element and returns the old value. The list becomes [10, 25, 30].
More AP Computer Science A
- Unit 1: Primitive Types, Variables, and Expressions
- Unit 2: Using Objects: Strings, Math, and Wrappers
- Unit 3: Boolean Expressions and if Statements
- Unit 4: Iteration
- Unit 5: Writing Classes
- Unit 6: Arrays
- Unit 8: 2D Arrays
- Unit 9: Recursion, Searching, and Sorting
Studying for a test in your own class? Make a practice test from your class notes.