2D Array Traversals FRQ Practice

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

Question 1 of 5

A camp lantern grid is walked in row-major order.

int[][] lamps = { {9, 2, 4}, {1, 8, 6} };
for (int r = 0; r < lamps.length; r++) {
    for (int c = 0; c < lamps[0].length; c++) {
        System.out.print(lamps[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, 1) cannot print before (0, 2).

Question 2 of 5

The lantern grid is walked in column-major order.

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

(a) List the (r, c) pairs in inner-body order. (b) Write the exact text printed, including spaces. (c) Compare the position of (1, 0) in this order with its position in row-major order.

Question 3 of 5

A snack cart uses nested enhanced-for loops.

int[][] cart = { {3, 5, 1}, {4, 2, 8} };
for (int[] row : cart) {
    for (int price : row) {
        System.out.print(price);
    }
    System.out.println();
}

(a) State the type of row and the type of price. (b) Write the two lines of output. (c) Explain why assigning price = 0 inside the inner loop would not change cart.

Question 4 of 5

A greenhouse tries two different enhanced-for updates.

int[][] vents = { {2, 4, 6}, {1, 3, 5} };
for (int[] row : vents) {
    row = new int[] {0, 0, 0};
}
for (int[] row : vents) {
    row[0] = 9;
}

(a) State vents[0][1] after both loops. (b) State vents[0][0] and vents[1][0] after both loops. (c) Explain the effect of row = new int[] {0, 0, 0}. (d) Explain the effect of row[0] = 9.

Question 5 of 5

A ferry cargo door is listed in two visit orders. Do not rotate the grid.

row C0 C1
R0 8 1
R1 5 3
R2 2 9

(a) Write nested for loops that print row-major 815329 with no spaces. (b) Write nested for loops that print column-major 852139 with no spaces. (c) State the third value printed by each version. (d) Explain why those third values differ using which index is outer.