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

  1. 7.1 ArrayList basics
  2. 7.2 Traversing ArrayLists
  3. 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

  1. Which statement creates an empty ArrayList of Strings?

    1. ArrayList list = new String[];
    2. ArrayList<String> list = new ArrayList<String>();
    3. ArrayList<String> list = new ArrayList<String>[10];
    4. String<ArrayList> list = new ArrayList();

    Answer: B. ArrayList<String> list = new ArrayList<String>(); Generic type parameters go in angle brackets.

  2. Which loop correctly prints every element of ArrayList<String> list?

    1. for (int i = 0; i < list.length; i++) System.out.print(list[i]);
    2. for (int i = 0; i <= list.size(); i++) System.out.print(list.get(i));
    3. for (int i = 0; i < list.size(); i++) System.out.print(list.get(i));
    4. 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.

  3. Which is an advantage of an ArrayList over an array?

    1. An ArrayList can hold primitive types such as int directly, without wrappers
    2. An ArrayList uses square brackets for indexing, which is faster than get
    3. An ArrayList has a fixed size, which prevents index errors at run time
    4. 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.

  4. Which statement is NOT valid?

    1. ArrayList<int> nums = new ArrayList<int>();
    2. ArrayList<Integer> nums = new ArrayList<Integer>();
    3. ArrayList<Double> vals = new ArrayList<Double>();
    4. 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.

  5. Removing elements from an ArrayList while traversing it with an enhanced for loop

    1. causes a ConcurrentModificationException
    2. works correctly and removes the elements
    3. removes every element in the list
    4. 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.

  6. For an ArrayList<Integer> nums, the expression nums.get(0) + 1

    1. causes a compile-time error
    2. unboxes the Integer to an int and adds 1
    3. concatenates "1"
    4. 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.

  7. After list.add("a"); list.add("b"); list.add(1, "c");, the list is

    1. [a, b, c]
    2. [c, a, b]
    3. [a, c, b]
    4. [a, c]

    Answer: C. [a, c, b] add(1, "c") inserts at index 1 and shifts b right.

  8. 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?

    1. It removes the odd numbers instead, because % 2 == 0 tests for odd
    2. It causes an infinite loop, because list.size() never changes
    3. After a removal, the next element shifts into index i and is skipped
    4. 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.

  9. Calling list.remove(Integer.valueOf(7)) on an ArrayList<Integer>

    1. removes the first element equal to 7
    2. removes the element at index 7
    3. removes every element equal to 7
    4. causes a compile-time error

    Answer: A. removes the first element equal to 7 remove(Object) removes by value; remove(int) removes by index.

  10. For an ArrayList list containing [10, 20, 30], what does list.set(1, 25) return?

    1. 25
    2. true
    3. 20
    4. Nothing; it is void

    Answer: C. 20 set replaces the element and returns the old value. The list becomes [10, 25, 30].

Practice all 283 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.