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
All Questions (9)
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.
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) 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.
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).
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) 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.
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.
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.
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).