Skip to main content

Unit 4 · Data Collections

Array Traversal

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What array traversal covers

Standard traversal runs an index from 0 while it is less than length, visiting every element once. The enhanced for loop gives read-only access to each element without an index and is the cleaner choice when position does not matter. Choosing between them is a real decision: you need the indexed form whenever you must write to the array, compare neighbors, or know where a value sits.

Where students lose points

Using <= length throws an ArrayIndexOutOfBoundsException, and starting at 1 silently skips the first element. Students also reach for the enhanced for loop when the task requires modification, then cannot explain why the array is unchanged afterward.

How it shows up on the AP exam

Unit 4 material and the backbone of array FRQs. Almost every array question begins with a traversal, so a wrong bound costs points before the interesting logic even starts.

For practice use only.

Array Traversal MCQ Practice

AP CSA array traversal practice: indexed versus enhanced for loops, summing and counting elements, and reading exactly the right range of indexes.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Which option matches the complete output produced by this segment?

public class ArrayTraversalContext
{
    public static void runExample()
    {
        int[] a = {4, 1, 6};
        int s = 0;
        for (int v : a)
        {
            s += v;
        }
        System.out.print(s);
    }

    public static void main(String[] args)
    {
        runExample();
    }
}
Choose one answer.