AP Computer Science A • Unit 4.11

2D Array Creation and Access

Detailed answer explanations for the Unit 4.11 multiple-choice practice questions.

Q1C
Q2D
Q3B
Q4B
Q5A
Q6A
Q7C
Q8D
Q9A
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Which expression creates an int array with 3 rows and 4 columns?

Anew int[4][3]
Bnew int(3, 4)
Cnew int[3][4]
Dnew int[3, 4]
Correct answer: C. The creation syntax is new datatype[rows][cols], so the row count is written first and the column count second.
Explanation

A 2D array is an array of arrays: the outer array holds the rows, and each inner array is one row whose elements form the columns. The general syntax datatype[][] name = new datatype[rows][cols]; fixes both counts at creation, and each dimension gets its own pair of square brackets.

  • A is not correct: This reverses the two dimensions and builds 4 rows of 3 columns, since the first number in the brackets is always the row count.
  • B is not correct: Array creation does not use a parenthesized argument list; the dimensions belong inside square brackets after new int.
  • D is not correct: Both dimensions are crammed into one bracket pair, but each dimension needs its own pair, as in new int[rows][cols].
Q2

Which statement about a 2D array is true?

AIts row count grows when a value is assigned.
BIt can store only primitive values.
CIt can store only object references.
DIts dimensions are fixed when it is created.
Correct answer: D. The two dimensions are set when the array is created, and the row and column counts do not change afterward.
Explanation

Creating a 2D array with new datatype[rows][cols] commits to a shape. Assigning into a cell only replaces the value stored in that cell; it never adds a row or a column. A 2D array may hold primitive values or object references, but either way its size is fixed.

  • A is not correct: Assigning a value only changes what one cell contains; the row count was fixed at creation and does not grow.
  • B is not correct: A 2D array may store primitive values, but it may also store object references, as String[][] seatingChart shows.
  • C is not correct: Object references are one option, yet arrays such as int[][] ticketInfo store primitive values instead.
Q3

What is the initial value of s[1][3]?

String[][] s = new String[2][4];
A""
Bnull
C0
D"null"
Correct answer: B. String is a reference type, and cells for reference types start out holding null.
Explanation

When a 2D array is created with new, every cell is filled with the default value for its type. int cells contain 0, double cells contain 0.0, boolean cells contain false, and cells for reference types contain null. No String object has been placed in the array yet.

  • A is not correct: An empty string is still a String object, and creating the array does not build any object for the cells.
  • C is not correct: 0 is the default that fills new int cells, not cells declared to hold a reference type.
  • D is not correct: This is a String made of the four letters n, u, l, l, which is different from the value null.
Q4

Given double[][] data, what is the type of data[1]?

Adouble
Bdouble[]
Cdouble[][]
Dint[]
Correct answer: B. Removing one bracket pair from a double[][] leaves a double[], which is one entire row.
Explanation

Track the type as you remove bracket pairs. If grid is an int[][], then grid[row] is an int[] and grid[row][col] is an int. The same counting applies here: one index selects a whole row, and a row is itself a 1D array.

  • A is not correct: That is the type of a single cell such as data[1][0], which needs both a row index and a column index.
  • C is not correct: That is the type of data itself, before any index has been applied to select a row.
  • D is not correct: The element type comes from the declaration, and the rows of a double[][] are arrays of double.
Q5

Which statement stores 12 at row 2, column 1 of points?

Apoints[2][1] = 12;
Bpoints[1][2] = 12;
Cpoints[2] = 12;
Dpoints.set(2, 1, 12);
Correct answer: A. Read arr[row][col] from left to right, so row 2 goes in the first bracket pair and column 1 in the second.
Explanation

The first index selects the row, and the second selects a column inside that row. Setting a cell means naming both indexes on the left of the assignment, exactly as in ticketInfo[0][0] = 15;.

  • B is not correct: This is the common mistake of writing [col][row]; it stores the value at row 1, column 2 instead.
  • C is not correct: One bracket pair selects an entire row, which is a 1D array, so a single number cannot be assigned to it.
  • D is not correct: Arrays are indexed with square brackets rather than method calls, so there is no such method to set a cell.
Q6

Which pair gives the row and column counts of f?

boolean[][] f = new boolean[4][2];
Af.length, then f[0].length
Bf[0].length, then f.length
Cf.length(), then f[0].length()
Df.size(), then f[0].size()
Correct answer: A. f.length counts the rows of the outer array and f[0].length counts the columns inside the first row.
Explanation

The outer array stores rows, so its length is the number of rows. Indexing once gives a row, and that row's length is the number of columns. More generally, grid[row].length gives the number of columns in the selected row.

  • B is not correct: The two values are in the wrong order here, because the count taken after indexing a row is the column count.
  • C is not correct: length is an array field, so it is written with no parentheses; adding them makes it look like a method call.
  • D is not correct: Arrays do not use size(); the number of rows and columns both come from the length field.
Q7

Which access is out of bounds?

int[][] board = new int[2][5];
Aboard[1][4]
Bboard[0][4]
Cboard[2][0]
Dboard[1][0]
Correct answer: C. Valid row indexes run from 0 through board.length - 1, which is 0 to 1, so a row index of 2 is outside the range.
Explanation

For a rectangular 2D array, valid row indexes are 0 through grid.length - 1 and valid column indexes in a selected row are 0 through grid[row].length - 1. If either index falls outside its range, Java throws ArrayIndexOutOfBoundsException at run time.

  • A is not correct: Row 1 is the last valid row and column 4 is the last valid column of five, so both indexes are in range.
  • B is not correct: Row 0 is valid, and column 4 equals board[0].length - 1, the largest legal column index.
  • D is not correct: Both indexes are the smallest legal values here, and index 0 is always in range for an array with rows and columns.
Q8

How many rows and columns does c have?

int[][] c = {{7,8}, {2,5}, {9,1}};
A2 rows and 2 columns
B2 rows and 3 columns
C3 rows and 3 columns
D3 rows and 2 columns
Correct answer: D. A 2D initializer list contains one inner list per row, and there are three inner lists holding two values each.
Explanation

Nested initializer lists supply values row by row. Counting the inner lists gives the number of rows, and counting the values inside one inner list gives the number of columns. This matches String[][] seating, where three inner lists of two names give seating.length of 3 and seating[0].length of 2.

  • A is not correct: There are three inner initializer lists, not two, so the row count is larger than the column count here.
  • B is not correct: This reads the counts backward; the number of inner lists is the row count and the values inside one list are the columns.
  • C is not correct: The row count is right, but each inner list holds only two values, so there are two columns rather than three.
Q9

Consider the following code segment. What is printed?

int[][] t = {{8,3}, {6,1}, {4,9}};
t[1][0] = 7;
System.out.println(t.length);
System.out.println(t[1][0]);
A3 and then 7
B2 and then 7
C3 and then 6
D2 and then 6
Correct answer: A. The initializer list gives three rows, and the assignment replaces the cell at row 1, column 0 before it is printed.
Explanation

t.length reports the number of inner lists in the initializer, which is 3. The statement t[1][0] = 7; sets one cell, so the later read of t[1][0] returns the stored value rather than the original one from the initializer list.

  • B is not correct: t.length is the row count, and the initializer list supplies three rows, not two.
  • C is not correct: The row count is right, but 6 was overwritten when 7 was assigned to that same cell.
  • D is not correct: Both values are wrong: the row count is three, and the cell was changed before it was printed.