AP Computer Science A · Topic 8.1
2D Arrays: Creation and Access Practice
Part of 2D Array.
Practice questions
27
Sample questions
5 of 27 — sign in to practice the rest with adaptive difficulty and mastery tracking.
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};
- Dcheck_circle
int[][] a = {{1,2,3},{4,5,6}};
Why
Nested braces: outer = rows; inner = columns.
- A
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
- Dcheck_circle
bc
Why
s[0][1] is "b" and s[1][0] is "c". Concatenation gives "bc".
- A
Sample 3difficulty 2/5
Which declares a 3×4 int matrix?
- A
int[3][4] m;
- Bcheck_circle
int[][] m = new int[3][4];
- C
int[][] m = new int(3,4);
- D
int m[3,4];
Why
Java syntax: <code>int[][] m = new int[rows][cols];</code>.
- A
Sample 4difficulty 2/5
int[][] m = {{1, 2, 3}, {4, 5, 6}}; System.out.println(m[1][2]);What is printed?
- Acheck_circle
6
- 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.
- A
Sample 5difficulty 2/5
int[][] m = new int[2][3]; System.out.println(m.length + " " + m[0].length);What is printed?
- Acheck_circle
2 3
- 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.
- A