AP CSA Unit 8: 2D Arrays

260 multiple-choice questions on AP Computer Science A Unit 8, 2D Arrays, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 6 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 8.1 Creating and accessing 2D arrays
  2. 8.2 Traversing 2D arrays
  3. 8.3 2D array algorithms

Key terms

8.3 2D array algorithms

2D array
An array of arrays representing rows and columns
g.length
The number of rows in a 2D array
Row-major order
Traversal that finishes each row before moving to the next
Column-major order
Traversal that finishes each column before moving to the next
Main diagonal
Elements g[i][i] of a square array
Transpose
Swapping rows and columns: element [r][c] moves to [c][r]

Sample practice questions

  1. Which statement creates a 2D array of ints with 3 rows and 4 columns?

    1. int[][] grid = new int[4][3];
    2. int[][] grid = new int[3][4];
    3. int[] grid = new int[3][4];
    4. int grid[][] = new int[3, 4];

    Answer: B. int[][] grid = new int[3][4]; The first size is the number of rows and the second the number of columns.

  2. Row-major order traverses a 2D array by

    1. visiting every element of column 0, then every element of column 1, and so on
    2. visiting every element of row 0, then every element of row 1, and so on
    3. visiting only the elements on the main diagonal, from top left to bottom right
    4. visiting the elements of the last row first, then working up toward row 0

    Answer: B. visiting every element of row 0, then every element of row 1, and so on The outer loop goes over rows and the inner loop over columns.

  3. Which code counts the elements of int[][] g that are greater than 10?

    1. int n = 0; for (int[] row : g) if (row > 10) n++;
    2. int n = 0; for (int[] row : g) for (int v : row) if (v > 10) n++;
    3. int n = 0; for (int v : g) if (v > 10) n++;
    4. int n = g.length; for (int[] row : g) n += row.length;

    Answer: B. int n = 0; for (int[] row : g) for (int v : row) if (v > 10) n++; Each element must be visited with nested loops; the outer loop variable is a row array.

  4. For int[][] grid = new int[3][4];, what is grid.length?

    1. 3
    2. 4
    3. 12
    4. 7

    Answer: A. 3 grid.length is the number of rows.

  5. Which nested loop traverses int[][] g in column-major order?

    1. for (int r = 0; r < g.length; r++) for (int c = 0; c < g[0].length; c++) use g[r][c]
    2. for (int c = 0; c < g.length; c++) for (int r = 0; r < g[0].length; r++) use g[r][c]
    3. for (int c = 0; c < g[0].length; c++) for (int r = 0; r < g.length; r++) use g[r][c]
    4. for (int r = 0; r < g.length; r++) use g[r][r]

    Answer: C. for (int c = 0; c < g[0].length; c++) for (int r = 0; r < g.length; r++) use g[r][c] The outer loop goes over columns; for each column, the inner loop goes down the rows.

  6. What does this code do? for (int r = 0; r < g.length; r++) for (int c = 0; c < g[0].length; c++) g[r][c] = r * c;

    1. Fills each element with the product of its row and column indices
    2. Fills each element with the sum of its row and column indices
    3. Fills each element with its row index, repeated across the row
    4. Doubles every element in the array, row by row

    Answer: A. Fills each element with the product of its row and column indices Each element is set to r * c, so row 0 and column 0 are all zeros.

  7. For int[][] grid = new int[3][4];, what is grid[0].length?

    1. 4
    2. 3
    3. 12
    4. 0

    Answer: A. 4 grid[0] is the first row, an array of 4 ints.

  8. What is printed? int[][] m = {{1, 2}, {3, 4}}; for (int[] row : m) for (int v : row) System.out.print(v);

    1. 1324
    2. 12
    3. 4321
    4. 1234

    Answer: D. 1234 Nested enhanced for loops traverse in row-major order.

  9. A method returns the largest value in a 2D array of ints. The best initial value for the running maximum is

    1. 0
    2. g[0][0]
    3. g.length
    4. −1

    Answer: B. g[0][0] Starting with an actual element works even if all values are negative.

  10. Given int[][] m = {{1, 2, 3}, {4, 5, 6}};, what is m[1][2]?

    1. 5
    2. 6
    3. 2
    4. 3

    Answer: B. 6 Row 1 is {4, 5, 6}; column 2 of that row is 6.

Practice all 260 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.