PrepGo

AP Computer Science A Practice Quiz: ArrayList Traversals

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

According to the provided text, what is the definition of traversing an ArrayList?

All Questions (10)

According to the provided text, what is the definition of traversing an ArrayList?

A) The process of declaring and initializing an ArrayList with values.

B) The use of iteration or recursive statements to access an ordered sequence of elements.

C) The act of sorting the elements within an ArrayList in ascending order.

D) The technique used to increase the capacity of an ArrayList.

Correct Answer: B

The content explicitly states, 'Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements in an ArrayList.'

A programmer writes code to iterate through an ArrayList of strings using an enhanced for loop. Inside the loop, if a string meets a certain condition, the code attempts to remove that string from the list. What is the most likely outcome of running this code?

A) The code will execute without errors, but it may skip checking the element immediately following the removed element.

B) The code will compile but will result in a ConcurrentModificationException at runtime.

C) The code will fail to compile because elements cannot be removed from within a loop.

D) The code will run successfully, correctly removing all targeted elements without any side effects.

Correct Answer: B

The content states, 'Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException.' Removing an element changes the size, which is not permitted during this type of traversal.

Why does deleting elements during a standard forward traversal of an ArrayList (e.g., using a for loop with an index from 0 to size-1) require 'special techniques'?

A) To prevent a ConcurrentModificationException from being thrown.

B) To avoid an IndexOutOfBoundsException when the last element is removed.

C) To ensure the loop does not skip evaluating the element that shifts into the current index after a deletion.

D) To improve the runtime efficiency of the removal process.

Correct Answer: C

The content mentions that special techniques are needed to avoid skipping elements. When an element at index `i` is removed, the element that was at `i+1` shifts to index `i`. If the loop then increments `i`, it will move to `i+1`, effectively skipping the element that just shifted into the current position.

Based on the provided guidelines, which of the following actions is explicitly advised against?

A) Using a recursive method to access all elements of an ArrayList.

B) Using an enhanced for loop to print every element in an ArrayList.

C) Adding an element to an ArrayList while iterating over it with an enhanced for loop.

D) Accessing an ordered sequence of elements without visiting all of them.

Correct Answer: C

The content explicitly warns against modifying the size of an ArrayList during an enhanced for loop traversal: '...when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.'

Which of the following describes a situation that does NOT violate the principles for ArrayList traversal outlined in the text?

A) A code segment uses an enhanced for loop to find and remove all negative numbers from an ArrayList of integers.

B) While iterating through an ArrayList with an enhanced for loop, new elements are added to the end of the list.

C) A traversal is performed where only the elements at even indices of an ArrayList are accessed.

D) A standard for loop iterates from 0 to list.size()-1, and elements are removed at the current index `i` without adjusting `i`.

Correct Answer: C

The content defines traversal as accessing 'all or an ordered sequence of the elements'. Accessing only elements at even indices fits this definition. Options A and B violate the rule about modifying an ArrayList during an enhanced for loop. Option D describes the exact problem (skipping elements) that requires special techniques.

Consider two scenarios for traversing an ArrayList: (I) Using a standard for loop with an index. (II) Using an enhanced for loop. In which of these scenarios can removing an element during traversal lead to a ConcurrentModificationException?

A) I only

B) II only

C) Both I and II

D) Neither I nor II

Correct Answer: B

The provided text specifically links the ConcurrentModificationException to changing the size of an ArrayList while using an enhanced for loop. While removing elements during a standard for loop can cause logical errors like skipping elements, it does not typically throw this specific exception.

Which two general programming constructs are mentioned for performing an ArrayList traversal?

A) if/else statements and switch statements

B) try/catch blocks and finalizers

C) class definitions and method signatures

D) iteration statements and recursive statements

Correct Answer: D

The text states, 'Traversing an ArrayList is when iteration or recursive statements are used to access all or an ordered sequence of the elements...'

A developer needs to remove all elements from an ArrayList that are less than 10. Which approach is problematic according to the provided text?

A) Using a special technique, such as iterating backwards, to remove elements.

B) Using an enhanced for loop to identify and remove the elements in a single pass.

C) First, traversing the list to find the indices of elements to remove, and then removing them in a separate loop.

D) Using recursion to process the list elements.

Correct Answer: B

The text explicitly warns that changing the size of an ArrayList (by removing elements) while traversing it with an enhanced for loop can cause a ConcurrentModificationException and should be avoided.

The text implies a key difference between traversing with a standard for loop versus an enhanced for loop when modifying the list. What is this key difference?

A) A standard for loop is always faster than an enhanced for loop.

B) Modifying the list during a standard for loop can cause logical errors (like skipping elements), while doing so in an enhanced for loop can cause a runtime crash (an exception).

C) Only an enhanced for loop can be used to access every element; a standard for loop cannot.

D) It is safe to add elements during a standard for loop traversal, but not during an enhanced for loop traversal.

Correct Answer: B

The text distinguishes between the two problems: for general traversals (like a standard loop), deleting requires techniques to 'avoid skipping elements' (a logical error). For an enhanced for loop, it explicitly states that modification can result in a 'ConcurrentModificationException' (a runtime error/crash).

What is the primary purpose of developing code to traverse an ArrayList?

A) To determine the results of the traversal, such as finding a value or computing a sum.

B) To ensure the ArrayList does not exceed its memory allocation.

C) To convert the ArrayList into a standard array.

D) To prevent ConcurrentModificationExceptions from occurring.

Correct Answer: A

The first point of the content is to 'Develop code used to traverse the elements of an ArrayList and determine the results of these traversals.' This indicates the purpose is to process the elements to achieve a result.