Unit 4 · Data Collections
ArrayList Traversal
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What arraylist traversal covers
Traversing an ArrayList uses either an index from 0 while it is less than size(), or an enhanced for loop over the elements. The indexed form is required whenever you need the position or plan to call set. The enhanced form is cleaner for read-only work such as summing, counting, or finding a maximum, but structurally modifying the list while an enhanced for loop is running is unsafe.
Where students lose points
Calling size() once and caching it, then adding or removing inside the loop, produces stale bounds. Students also mix the two loop styles in one traversal, and they use get(i) with a bound of <= size(), which throws an IndexOutOfBoundsException.
How it shows up on the AP exam
Unit 4 content that combines with accumulator patterns from Unit 2. Most ArrayList FRQ parts are a traversal with one decision inside it.
For practice use only.
ArrayList Traversal MCQ Practice
AP CSA ArrayList traversal practice: indexed and enhanced for loops over lists, accumulating results, and avoiding modification during iteration.
Answered 0 of 15
Choose one answer.
Which option matches the complete output produced by this segment?
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(4);
a.add(1);
a.add(6);
int c = 0;
for (int v : a)
{
if (v > 3)
{
c++;
}
}
System.out.print(c);