Skip to main content

Unit 4 · Data Collections

2D Array Traversal

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

What 2d array traversal covers

Row-major traversal puts the row loop outside and the column loop inside, visiting an entire row before moving down. Swapping the loops gives column-major order, which visits one column top to bottom before moving right. The enhanced form nests a loop over each row array inside a loop over the grid. Per-row work such as a row sum requires resetting the accumulator at the top of each outer iteration.

Where students lose points

Using the outer loop variable to index columns, or bounding the inner loop with grid.length instead of grid[r].length, breaks any non-square grid. Declaring a per-row accumulator before the outer loop makes it a grand total instead of a row total, which is a common silent wrong answer.

How it shows up on the AP exam

Unit 4 material combining 2D arrays with the nested loops from Unit 2. Traversal order questions and per-row computation are both standard FRQ tasks.

For practice use only.

2D Array Traversal MCQ Practice

AP CSA 2D array traversal practice: nested row-major loops, column-major order, enhanced for loops over rows, and per-row accumulators.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Which option matches the complete output produced by this segment?

public class GridArrayTraversalContext
{
    public static void runExample()
    {
        int[][] g = {{1, 2}, {3, 4}};
        int s = 0;
        for (int r = 0; r < g.length; r++)
        {
            for (int c = 0; c < g[r].length; c++)
            {
                s += g[r][c];
            }
        }
        System.out.print(s);
    }

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