2D Array Traversals FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

A festival booth map is walked in row-major nested for loops.

int[][] booths = { {4, 7, 1}, {8, 3, 6} };
for (int r = 0; r < booths.length; r++) {
    for (int c = 0; c < booths[0].length; c++) {
        System.out.print(booths[r][c]);
        System.out.print(" ");
    }
}
Visit number row r col c Value printed
1
2
3
4
5
6

(a) Complete the visit table. (b) Write the exact text printed, including spaces. (c) Explain why (1, 0) cannot print before (0, 2).

Question 2 of 5

The same booth map is walked in column-major nested for loops.

int[][] booths = { {4, 7, 1}, {8, 3, 6} };
for (int c = 0; c < booths[0].length; c++) {
    for (int r = 0; r < booths.length; r++) {
        System.out.print(booths[r][c]);
        System.out.print(" ");
    }
}

(a) List the (r, c) pairs in the order the inner body runs. (b) Write the exact text printed, including spaces. (c) Name one pair that appears earlier in this order than in row-major order, and explain using the outer loop variable.

Question 3 of 5

A tide chart uses nested enhanced-for loops.

int[][] tides = { {2, 9}, {4, 1}, {8, 5} };
for (int[] row : tides) {
    for (int n : row) {
        System.out.print(n);
    }
    System.out.println();
}

(a) State the type of row and the type of n. (b) Write the three lines of output. (c) Explain why this nested enhanced-for pair visits in row-major order rather than column-major order.

Question 4 of 5

Two lantern loops look similar but are not.

int[][] grid = { {2, 5}, {1, 8} };
for (int n : grid[0]) {
    n = 0;
}
for (int[] row : grid) {
    row[1] = 9;
}

(a) State grid[0][0] after both loops. (b) State grid[0][1] and grid[1][1] after both loops. (c) Explain why n = 0 does not change stored values. (d) Explain why row[1] = 9 does change stored values.

Question 5 of 5

A solar panel is printed two ways. Do not rotate or rewrite the stored grid.

row C0 C1 C2
R0 3 6 9
R1 2 4 8

(a) Write nested for loops that print the values in row-major order as 369248 with no spaces. (b) Write nested for loops that print the values in column-major order as 326498 with no spaces. (c) State the second value printed by each version. (d) Explain the difference using which index the outer loop controls.