PrepGo

AP Computer Science A Practice Quiz: 2D Array Creation and Access

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 10 questions to check your progress.

Question 1 of 10

Which of the following code snippets correctly creates a 2D array of integers named `grades` with 10 rows and 5 columns?

All Questions (10)

Which of the following code snippets correctly creates a 2D array of integers named `grades` with 10 rows and 5 columns?

A) int[][] grades = new int[10][5];

B) int grades[][] = new int[5][10];

C) int[10][5] grades = new int[][];

D) int[][] grades = new int[];

Correct Answer: A

The correct syntax for creating a 2D array is `type[][] arrayName = new type[rows][columns];`. Option A correctly specifies 10 for the number of rows (the first index) and 5 for the number of columns (the second index).

Consider the following 2D array declaration: `String[][] words = {{"a", "b", "c"}, {"d", "e", "f"}};` Which expression correctly accesses the element "e"?

A) words[1][1]

B) words[2][2]

C) words[1, 1]

D) words[e]

Correct Answer: A

2D arrays are accessed using the format `[row][col]`. Since array indexing is 0-based, the element "e" is located in the second row (index 1) and the second column (index 1). Therefore, `words[1][1]` is the correct expression.

Given the 2D array `double[][] matrix = new double[25][50];`, which expression evaluates to the number of rows in `matrix`?

A) matrix.length

B) matrix[0].length

C) matrix.length()

D) matrix[25].length

Correct Answer: A

For a 2D array, the `length` attribute of the array object itself provides the number of rows. `matrix.length` will correctly evaluate to 25.

A 2D array `board` is created to represent a game board. Assuming the array is not empty and all rows have the same number of columns, which expression correctly determines the number of columns?

A) board.length

B) board[0].length

C) board.length[0]

D) board[].length

Correct Answer: B

A 2D array is an array of arrays. To find the number of columns, you must first access one of the inner arrays (a row, such as `board[0]`) and then get its `length` attribute.

Which statement best describes the underlying structure of a 2D array in Java?

A) A single, contiguous block of memory organized as a grid.

B) An array where each element is a reference to another array.

C) A special object that can dynamically change its number of rows and columns.

D) A collection that stores key-value pairs in a grid format.

Correct Answer: B

The provided content explicitly states, 'A 2D array is stored as an array of arrays.' This means the outer array contains elements that are references to the inner arrays (the rows).

Consider the following code segment: ```java int[][] data = {{10, 20}, {30, 40}, {50, 60}}; data[1][0] = 99; System.out.println(data[1][0]); ``` What is printed as a result of executing the code segment?

A) 20

B) 30

C) 40

D) 99

Correct Answer: D

The statement `data[1][0] = 99;` uses the square brackets `[row][col]` to access the element at row index 1 and column index 0 (which is initially 30) and modifies its value to 99. The subsequent print statement outputs this new value.

A 2D array is declared as `char[][] grid = new char[5][5];`. Which of the following statements is true about the `grid` array?

A) The number of rows can be changed to 6 after creation.

B) The number of columns in the first row can be changed to 7.

C) The size of the `grid` is established at creation and cannot be changed.

D) New rows can be added, but existing rows cannot be resized.

Correct Answer: C

The provided content states, 'The size of a 2D array is established at the time of creation and cannot be changed.' This means that both the number of rows and the number of columns are fixed once the array is instantiated.

A programmer creates a 2D array: `String[][] userNames = new String[50][2];`. Which statement accurately describes the data that can be stored in this array?

A) Only primitive data can be stored.

B) Only object reference data can be stored.

C) Both primitive `char` data and `String` objects can be stored.

D) The array can store any type of object reference, such as `Integer` or `Double`.

Correct Answer: B

The array is declared with the type `String`, which is an object. Therefore, it can only hold references to `String` objects. The content states that 2D arrays can store either primitive data or object reference data, and the type is fixed upon declaration.

Consider the following 2D array: ```java String[][] schedule = { {"Math", "Sci", "Hist"}, {"Eng", "Art", "Gym"}, {"Lang", "Music", "Study"} }; ``` What is the value of the expression `schedule[schedule.length - 1][0]`?

A) "Hist"

B) "Eng"

C) "Lang"

D) "Study"

Correct Answer: C

`schedule.length` evaluates to 3, which is the number of rows. Therefore, `schedule.length - 1` is 2. The expression simplifies to `schedule[2][0]`, which accesses the element at the last row (index 2) and the first column (index 0). This element is "Lang".

What is the value of `matrix[0][1]` after the following code segment is executed? ```java int[][] matrix = new int[2][2]; matrix[0][0] = 5; matrix[1][1] = 10; matrix[0][1] = matrix[0][0] + matrix[1][1]; matrix[1][0] = matrix[0][1] * 2; ```

A) 5

B) 10

C) 15

D) 30

Correct Answer: C

The code first initializes `matrix[0][0]` to 5 and `matrix[1][1]` to 10. The third line accesses these values and modifies `matrix[0][1]`, calculating `matrix[0][1] = 5 + 10`, which results in 15. The fourth line modifies a different element (`matrix[1][0]`), so the value of `matrix[0][1]` remains 15.