Implementing 2D Array Algorithms FRQ Practice

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

Question 1 of 5

A harbor lamp board stores brightness in a rectangular int[][] named grid. Write a method that reverses one entire row in place.

public static void reverseRow(int[][] grid, int row)
{
    /* complete the method */
}

(a) State which pair of indexes should be swapped first when the chosen row has length 4. (b) Write a loop that swaps from the ends toward the middle. (c) Give the row values after reverseRow is called on {2, 5, 9, 1}.

Question 2 of 5

A kiln log stores temperatures in a rectangular 3-by-3 grid. Complete the method so it returns the minimum value in one column.

public static int minInColumn(int[][] grid, int col)
{
    int min = grid[0][col];
    /* write the remaining statements */
    return min;
}

Use this grid for part (c):

row col 0 col 1 col 2
row 0 12 4 9
row 1 6 15 3
row 2 8 1 7

(a) Explain why min should start as grid[0][col] rather than 0. (b) Write the loop over the remaining rows. (c) State the value returned for col 1.

Question 3 of 5

A crate wall records integer weights. Write two related algorithms on the whole rectangular grid.

(a) Write a method countHeavy(int[][] grid, int cutoff) that returns how many cells are strictly greater than cutoff. (b) Write a method hasHeavy(int[][] grid, int cutoff) that returns true if at least one cell is strictly greater than cutoff. (c) Using {{4, 9}, {1, 6}} and cutoff 5, state both return values and explain how they are related.

Question 4 of 5

A elevator chute rotates one column downward: the bottom value moves to the top, and every other value in that column shifts down one row.

public static void rotateColumnDown(int[][] grid, int col)
{
    /* complete the method */
}

Column 0 starts as 5, 8, 2 from top to bottom.

(a) Identify which value must be saved before any assignment overwrites it. (b) Write the shift loop and the wrap assignment. (c) List column 0 from top to bottom after the rotate.

Question 5 of 5

A mural wall stores paint codes in a rectangular grid. Consecutive vertical pairs are cells in the same column and neighboring rows.

(a) Write a method that returns how many vertical consecutive pairs have equal values. (b) Explain why the outer loop may run only to grid.length - 1. (c) For the grid {{7, 1}, {7, 4}, {2, 4}}, state the returned count and list the matching pairs as (row, col) of the upper cell.