AP CSA Unit-Level MCQ

2D Array Basics

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What 2d array basics covers

A 2D array in Java is an array of arrays, stored in row-major order, so grid[r][c] means row r, column c. The number of rows is grid.length and the number of columns in a row is grid[r].length. Creating one with new int[3][4] gives three rows of four columns, all filled with the type default. Initializer lists let you write the grid out literally, with each inner set of braces forming one row.

Where students lose points

Reversing the two indexes is the defining mistake, and it either reads the wrong value or throws when the grid is not square. Students also use grid.length as the column count, which only happens to work for square grids, and they misread new int[3][4] as four rows of three.

How it shows up on the AP exam

Unit 4 data collections. 2D arrays appear as a full FRQ on a regular basis, and index order is the first thing that has to be right.

For practice use only.

2D Array Basics MCQ Practice

AP CSA 2D array basics practice: row-major indexing, declaring and initializing grids, row and column counts, and default element values.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - 2D Array Basics: track the final printed value.

int[][] grid = {{2, 5, 1}, {4, 3, 6}, {7, 0, 8}};
int total = 0;
for (int r = 0; r < grid.length; r++) {
  int c = (r + 0) % grid[r].length;
  total += grid[r][c] * (r + 1);
}
System.out.print(total);

All 15 2d array basics questions

Work through the interactive quiz above first. This is the full 2d array basics question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - 2D Array Basics: track the final printed value.

    int[][] grid = {{2, 5, 1}, {4, 3, 6}, {7, 0, 8}};
    int total = 0;
    for (int r = 0; r < grid.length; r++) {
      int c = (r + 0) % grid[r].length;
      total += grid[r][c] * (r + 1);
    }
    System.out.print(total);
    • 36
    • 32
    • 34
    • 29
    Show worked solution

    Correct answer: 32

    Each row chooses a column using modular arithmetic, then weights that cell by r + 1 before adding it to total.

  2. 2.Code Tracing 12 - 2D Array Basics: follow the variable updates.

    int[][] grid = {{4, 7, 3}, {6, 5, 8}, {9, 2, 10}};
    int main = 0;
    int anti = 0;
    for (int r = 0; r < grid.length; r++) {
      main += grid[r][r];
      anti += grid[r][grid[r].length - 1 - r];
    }
    System.out.print(main + ":" + anti + ":" + (main - anti));
    • 36:17:19
    • 19:17:-2
    • 19:17:2
    • 17:19:-2
    Show worked solution

    Correct answer: 19:17:2

    The loop updates both diagonals at the same time. The final expression prints the two totals and their difference.

  3. 3.Code Tracing 28 - 2D Array Basics: trace the branch and loop path.

    int[][] grid = {{7, 6, 1}, {6, 8, 11}, {7, 1, 13}};
    int count = 0;
    int total = 0;
    for (int r = 0; r < grid.length; r++) {
      for (int c = 0; c < grid[r].length; c++) {
        if (grid[r][c] >= 6) {
          count++;
          total += grid[r][c] - r;
        } else {
          total -= c;
        }
      }
    }
    System.out.print(count + ":" + total);
    • 6:48
    • 2:48
    • 7:48
    • 7:55
    Show worked solution

    Correct answer: 7:48

    The nested loops visit every cell. Matching cells increase both count and total, while nonmatching cells subtract their column index.

  4. 4.Code Tracing 39 - 2D Array Basics: watch the index changes.

    int[][] grid = {{9, 8, 3}, {5, 10, 7}, {9, 3, 15}};
    for (int r = 0; r < grid.length; r++) {
      for (int c = 0; c < grid[r].length; c++) {
        if ((r + c) % 2 == 0) {
          grid[r][c] += r + c;
        }
      }
    }
    System.out.print(grid[0][0] + "-" + grid[1][1] + "-" + grid[2][2]);
    • 9-10-15
    • 9-8-5
    • 11-3-19
    • 9-12-19
    Show worked solution

    Correct answer: 9-12-19

    Only cells whose row-plus-column index is even are changed. The printed diagonal includes three of those updated cells.

  5. 5.Code Tracing 50 - 2D Array Basics: evaluate the state change step by step.

    int[][] grid = {{11, 6, 5}, {4, 12, 9}, {11, 1, 17}};
    int total = 0;
    int count = 0;
    for (int c = 0; c < grid[1].length; c++) {
      if (grid[1][c] >= 10) {
        total += grid[1][c];
        count++;
      }
    }
    System.out.print(total + ":" + count);
    • 12:1
    • 25:3
    • 13:1
    • 22:1
    Show worked solution

    Correct answer: 12:1

    The loop traverses row 1, but only values meeting the threshold affect the total and count.

  6. 6.What is printed by the following code segment?

    int[][] g = new int[3][4];
    System.out.print(g.length + " " + g[0].length);
    System.out.print(" " + g[2][3]);
    • 4 3 0
    • 3 4 0
    • 12 4 0
    • 3 4 null
    Show worked solution

    Correct answer: 3 4 0

    Rows come first in the declaration and every element starts at zero.

  7. 7.What is printed by the following code segment?

    int[][] g = {{1, 2}, {3, 4, 5}};
    int t = 0;
    for (int r = 0; r < g.length; r++)
    {
        t += g[r].length;
    }
    System.out.print(t);
    • 4
    • 6
    • 5
    • 2
    Show worked solution

    Correct answer: 5

    Rows may differ in length, and summing the row lengths counts every element.

  8. 8.What is printed by the following code segment?

    int[][] g = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int t = 0;
    for (int i = 0; i < g.length; i++)
    {
        t += g[i][i] + g[i][g.length - 1 - i];
    }
    System.out.print(t);
    • 15
    • 25
    • 45
    • 30
    Show worked solution

    Correct answer: 30

    The two diagonals total 15 each, and the centre element is counted twice.

  9. 9.What is printed by the following code segment?

    int[][] g = new int[3][3];
    for (int r = 0; r < 3; r++)
    {
        for (int c = 0; c < 3; c++)
        {
            g[r][c] = r * 3 + c;
        }
    }
    System.out.print(g[1][2] + " " + g[2][0]);
    • 5 6
    • 6 5
    • 4 6
    • 5 7
    Show worked solution

    Correct answer: 5 6

    The formula numbers the cells in row-major order from zero.

  10. 10.What is printed by the following code segment?

    int[][] g = {{1, 2}, {3, 4}};
    int[] row = g[0];
    row[1] = 20;
    g[1] = row;
    System.out.print(g[0][1] + " " + g[1][1]);
    • 2 4
    • 20 20
    • 20 4
    • 2 20
    Show worked solution

    Correct answer: 20 20

    Assigning the row reference makes both rows the same array object.

  11. 11.What is printed by the following code segment?

    int[][] g = {{5, 1}, {2, 8}};
    int m = g[0][0];
    for (int[] row : g)
    {
        for (int v : row)
        {
            if (v > m) { m = v; }
        }
    }
    System.out.print(m);
    • 5
    • 2
    • 8
    • 16
    Show worked solution

    Correct answer: 8

    The enhanced form treats each row as an int array, and the search covers every cell.

  12. 12.What is printed by the following code segment?

    int[][] g = new int[2][3];
    int c = 0;
    for (int[] row : g)
    {
        c += row.length;
    }
    System.out.print(c + " " + g.length);
    • 5 2
    • 6 3
    • 3 2
    • 6 2
    Show worked solution

    Correct answer: 6 2

    Two rows of three cells give six elements in total.

  13. 13.What is the result of executing the following code segment?

    int[][] g = {{1, 2}, {3}};
    System.out.print(g[1][1]);
    • An ArrayIndexOutOfBoundsException is thrown.
    • 3 is printed.
    • 0 is printed.
    • The code does not compile.
    Show worked solution

    Correct answer: An ArrayIndexOutOfBoundsException is thrown.

    The second row holds only one element, so index 1 is out of range for that row.

  14. 14.What is printed by the following code segment?

    int[][] g = {{1, 2, 3}};
    int t = 0;
    for (int c = 0; c < g[0].length; c++)
    {
        t += g[0][c] * (c + 1);
    }
    System.out.print(t);
    • 6
    • 14
    • 9
    • 12
    Show worked solution

    Correct answer: 14

    Each element is weighted by its one-based column position.

  15. 15.What is printed by the following code segment?

    String[][] g = new String[2][2];
    g[0][0] = "a";
    int n = 0;
    for (String[] row : g)
    {
        for (String v : row)
        {
            if (v == null) { n++; }
        }
    }
    System.out.print(n);
    • 4
    • 1
    • 3
    • 0
    Show worked solution

    Correct answer: 3

    A grid of objects starts full of null and only one cell was assigned.