2D Arrays in AP Computer Science A
Which of the following is the correct way to declare and initialize a rectangular 2D array of integers with 3 rows and 4 columns?
int[][] arr = new int[4][3];
int[][] arr = new int[3, 4];
int[] arr[] = new int[3][4];
int[][] arr = new int[][3][4];
Consider the following code snippet:
java
int[][] matrix = new int[2][3];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
How are the elements of matrix
stored in memory...
As a single contiguous block of memory.
As a nested array, where each row is a separate array.
As a linked list of integers.
As a hash map with row and column indices as keys.
Given the following initialization of a 2D array, what is the value of array[1][2]
?
String[][] array = {{"apple", "banana", "cherry"}, {"date", "elderberry", "fig"}};
"banana"
"cherry"
"elderberry"
"fig"
Consider the following 2D array:
java
int[][] grid = {{1, 2, 3}, {4, 5, 6}};
Which representation best describes grid
in memory?
A single array containing the numbers 1 through 6.
An array of two elements, where each element is an array of three integers.
A linked list of arrays.
A hash table where keys are coordinates and values are integers.
What is the correct way to access the element in the second row and third column of the following 2D array?
int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
numbers[2][3]
numbers[1][2]
numbers[2, 3]
numbers(1)(2)
Consider the following code snippet:
java
int[][] values = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
sum += values[i][j];
}
}
System.out.println(sum);
...
6
21
24
45
What will happen when the following code is executed?
java
int[][] matrix = new int[3][4];
System.out.println(matrix[3][0]);
The program will print 0.
The program will print null.
The program will throw an ArrayIndexOutOfBoundsException.
The program will compile but produce no output.

How are we doing?
Give us your feedback and let us know how we can improve
Consider the following 2D array:
java
int[][] arr = {{1, 2, 3}, {4, 5, 6}};
What is the value of arr[0].length
?
1
2
3
6
How many rows does the following 2D array have?
int[][] myArray = new int[5][10];
5
10
15
50
Given the following code:
java
int[][] matrix = new int[2][2];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
matrix[1][1] = 4;
int[] arr = matrix[0];
arr[0] = 5;
System.out.println(matrix[0][0]);
What is printed to the cons...
1
2
3
5