AP Computer Science A · Topic 8.1

2D Arrays: Creation and Access Practice

Part of 2D Array.

Practice questions

27

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 27 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 2/5

    Which initializes:

    <code>1 2 3 4 5 6 </code></pre>

    • A

      int a[2][3] = {1,2,3,4,5,6};

    • B

      int[][] a = (1,2,3,4,5,6);

    • C

      int[][] a = new int[2][3]{1,2,3,4,5,6};

    • D

      int[][] a = {{1,2,3},{4,5,6}};

      check_circle

    Why

    Nested braces: outer = rows; inner = columns.

  2. Sample 2difficulty 2/5

    String[][] s = {{"a", "b"}, {"c", "d"}};
    System.out.println(s[0][1] + s[1][0]);

    What is printed?

    • A

      ac

    • B

      ab

    • C

      cd

    • D

      bc

      check_circle

    Why

    s[0][1] is "b" and s[1][0] is "c". Concatenation gives "bc".

  3. Sample 3difficulty 2/5

    Which declares a 3×4 int matrix?

    • A

      int[3][4] m;

    • B

      int[][] m = new int[3][4];

      check_circle
    • C

      int[][] m = new int(3,4);

    • D

      int m[3,4];

    Why

    Java syntax: <code>int[][] m = new int[rows][cols];</code>.

  4. Sample 4difficulty 2/5

    int[][] m = {{1, 2, 3}, {4, 5, 6}};
    System.out.println(m[1][2]);

    What is printed?

    • A

      6

      check_circle
    • B

      3

    • C

      5

    • D

      4

    Why

    m[1] is the second row {4, 5, 6}, and m[1][2] is the third element of that row, which is 6.

  5. Sample 5difficulty 2/5

    int[][] m = new int[2][3];
    System.out.println(m.length + " " + m[0].length);

    What is printed?

    • A

      2 3

      check_circle
    • B

      3 2

    • C

      2 2

    • D

      6 0

    Why

    new int[2][3] creates a 2-row, 3-column array. m.length is rows = 2, m[0].length is columns = 3.