ArrayList Methods, Traversals, and Algorithms
Free AP Computer Science A multiple-choice practice for topics 4.8–4.10, Unit 4 (Data Collections). Answer five random questions, check your work, and review the essential knowledge behind each one.
Unit 4: Data Collections. Topics 4.8–4.10. This set has 60 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.
Topics assessed
- 4.8 ArrayList Methods
- 4.9 ArrayList Traversals
- 4.10 Implementing ArrayList Algorithms
Practice questions
Loading questions…
Essential knowledge for this set
These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 60 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 4.8: ArrayList Methods
4.8.A — Develop code for collections of related objects using ArrayList objects and determine the result of calling methods on these objects.
- 4.8.A.1 An ArrayList object is mutable in size and contains object references.
- 4.8.A.2 The ArrayList constructor ArrayList() constructs an empty list.
- 4.8.A.3 Java allows the generic type ArrayList<E>, where the type parameter E specifies the type of the elements. When ArrayList<E> is specified, the types of the reference parameters and return type when using the ArrayList methods are type E. ArrayList<E> is preferred over ArrayList. For example, ArrayList<String> names = new ArrayList<String>(); allows the compiler to find errors that would otherwise be found at run-time.
- 4.8.A.4 The ArrayList class is part of the java.util package. An import statement must be used to make this class available for use in the program.
-
4.8.A.5 The following ArrayList methods— including what they do and when they are used—are part of the Java Quick Reference:
- int size() returns the number of elements in the list.
- boolean add(E obj) appends obj to end of list; returns true.
- void add(int index, E obj) inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size.
- E get(int index) returns the element at position index in the list.
- E set(int index, E obj) replaces the element at position index with obj; returns the element formerly at position index.
- E remove(int index) removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index.
- 4.8.A.6 The indices for an ArrayList start at 0 and end at the number of elements - 1.
Topic 4.9: ArrayList Traversals
4.9.A — Develop code used to traverse the elements of an ArrayList and determine the results of these traversals.
- 4.9.A.1 Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements in an ArrayList.
- 4.9.A.2 Deleting elements during a traversal of an ArrayList requires the use of special techniques to avoid skipping elements.
- 4.9.A.3 Attempting to access an index value outside of its range will result in an IndexOutOfBoundsException.
- 4.9.A.4 Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.
Topic 4.10: Implementing ArrayList Algorithms
4.10.A — Develop code for standard and original algorithms for a particular context or specification that involve ArrayList objects and determine the result of these algorithms.
-
4.10.A.1 There are standard ArrayList algorithms that utilize traversals to:
- determine a minimum or maximum value
- compute a sum or average
- determine if at least one element has a particular property
- determine if all elements have a particular property
- determine the number of elements having a particular property
- access all consecutive pairs of elements
- determine the presence or absence of duplicate elements
- shift or rotate elements left or right
- reverse the order of the elements
- insert elements
- delete elements
- 4.10.A.2 Some algorithms require multiple String, array, or ArrayList objects to be traversed simultaneously.