PrepGo

AP Computer Science A Practice Quiz: 2D Array Traversals

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

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

Question 1 of 9

Which of the following describes the process of traversing a 2D array by accessing all elements in the first row, followed by all elements in the second row, and so on, until the last row is processed?

All Questions (9)

Which of the following describes the process of traversing a 2D array by accessing all elements in the first row, followed by all elements in the second row, and so on, until the last row is processed?

A) Row-major order

B) Column-major order

C) Diagonal traversal

D) Reverse order

Correct Answer: A

The provided content states that nested iteration statements can traverse a 2D array in row-major order. This order involves processing each row completely before moving to the next, which matches the description.

Consider the following code segment intended to traverse a 2D array `grid`. ```java for (int[] row : grid) { for (int val : row) { System.out.print(val + " "); } } ``` What does the outer loop `for (int[] row : grid)` iterate through?

A) Each individual integer element in the entire 2D array.

B) The columns of the 2D array.

C) The rows of the 2D array, where each row is a 1D array.

D) The indices of the rows.

Correct Answer: C

According to the provided content, 'The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows.' Each row of a 2D array is itself a 1D array of elements.

A programmer wants to access all elements in a 2D array named `data` by processing the first column from top to bottom, then the second column from top to bottom, and so on. Which traversal method should be used?

A) Row-major order

B) Column-major order

C) A single enhanced for loop

D) A single standard for loop

Correct Answer: B

The content specifies that nested iteration statements can be written to traverse a 2D array in column-major order. This order processes all elements in a column before moving to the next column, which matches the requirement.

Consider the following 2D array and code segment. `int[][] nums = {{10, 20}, {30, 40}, {50, 60}};` ```java for (int c = 0; c < nums[0].length; c++) { for (int r = 0; r < nums.length; r++) { System.out.print(nums[r][c] + " "); } } ``` What is the output of this code?

A) 10 20 30 40 50 60

B) 10 30 50 20 40 60

C) 50 60 30 40 10 20

D) 10 40 50 20 30 60

Correct Answer: B

The outer loop iterates through the column index `c`, and the inner loop iterates through the row index `r`. This structure results in a column-major traversal. It prints the elements of the first column (10, 30, 50) and then the elements of the second column (20, 40, 60).

Which of the following code segments correctly calculates and prints the sum of all elements in a 2D integer array `matrix` using a row-major traversal?

A) ```java int sum = 0; for (int c = 0; c < matrix[0].length; c++) { for (int r = 0; r < matrix.length; r++) { sum += matrix[r][c]; } } System.out.println(sum); ```

B) ```java int sum = 0; for (int r = 0; r < matrix.length; r++) { for (int c = 0; c < matrix[r].length; c++) { sum += matrix[r][c]; } } System.out.println(sum); ```

C) ```java int sum = 0; for (int[] row : matrix) { sum += row; } System.out.println(sum); ```

D) ```java int sum = 0; for (int r = 0; r < matrix.length; r++) { sum += matrix[r]; } System.out.println(sum); ```

Correct Answer: B

The content states that nested iteration statements are used to traverse all elements. A row-major traversal requires the outer loop to iterate through the rows (indexed by `r`) and the inner loop to iterate through the columns (indexed by `c`). Option B correctly implements this structure to access every element `matrix[r][c]` and add it to the sum.

A programmer is using a nested enhanced for loop to traverse a 2D array. Which of the following statements is true regarding this approach?

A) It can be easily modified to traverse in column-major order.

B) The inner loop variable represents a 1D array corresponding to a column.

C) The traversal is always in row-major order.

D) It allows for direct modification of the array elements' indices.

Correct Answer: C

The provided content explicitly states: 'The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows. The inner loop traverses a single row.' This structure inherently processes the array row by row, which is the definition of row-major order. It cannot be easily changed to column-major order.

Consider the following code segment, which is intended to process a square 2D array `arr`. ```java int result = 0; for (int i = 0; i < arr.length; i++) { result += arr[i][arr.length - 1 - i]; } ``` What does this code segment calculate?

A) The sum of the elements in the first row.

B) The sum of the elements on the main diagonal (top-left to bottom-right).

C) The sum of the elements on the anti-diagonal (top-right to bottom-left).

D) The sum of the elements in the last column.

Correct Answer: C

This code uses a single loop to perform a uniquely defined traversal. For each row `i`, it accesses the element at column `arr.length - 1 - i`. When i=0, it accesses the last column. When i=1, it accesses the second-to-last column, and so on. This path traces the anti-diagonal from the top-right corner to the bottom-left corner, summing its elements.

What is the primary mechanism used to traverse and access all elements in an ordered sequence within a 2D array?

A) A single recursive method

B) Nested iteration statements

C) A single while loop

D) A switch statement

Correct Answer: B

The content directly states, 'Nested iteration statements are used to traverse and access all or an ordered sequence of elements in a 2D array.' This is the fundamental technique for processing every element in structures like 2D arrays.

Consider the following 2D array and code segment. `int[][] data = {{5, 3, 8}, {2, 6, 4}};` ```java for (int r = data.length - 1; r >= 0; r--) { for (int c = 0; c < data[0].length; c++) { System.out.print(data[r][c] + " "); } } ``` What is the output of this code?

A) 5 3 8 2 6 4

B) 8 3 5 4 6 2

C) 2 6 4 5 3 8

D) 4 6 2 8 3 5

Correct Answer: C

This code demonstrates a uniquely defined traversal order. The outer loop iterates through the rows in reverse order (from `data.length - 1` down to 0). The inner loop iterates through the columns in the standard forward order. Therefore, it will first print the last row (2 6 4) and then the first row (5 3 8).