2D Array Practice Questions (AP CSA)

Java MCQ with Answers


<-- Back to Using Loops Next to ArrayLists-->



Two-Dimensional Arrays in Java

Declaring and Initializing a 2D Array

A two-dimensional array (2D array) stores data in a grid of rows and columns. It can be declared and allocated with int[][] arr = new int[rows][cols];, which creates an array with the given number of rows and columns, with all elements initialized to their default values ( 0 for int, '\u0000' for char, etc.). Alternatively, it can be declared and initialized in one step using an initializer list: int[][] arr = { {1, 2}, {3, 4}, {5, 6} };, where each inner set of braces defines one row.

Accessing Elements — Zero-Based Indexing

Individual elements are accessed using two indices: arr[row][col]. Both row and column indices are zero-based — the first row is row 0 and the first column is column 0. For example, in a 3-row, 2-column array, the last element is at arr[2][1]. Using an index equal to or greater than the dimension's size causes an ArrayIndexOutOfBoundsException.

Finding the Size — .length

The number of rows in a 2D array arr is given by arr.length, and the number of columns in the first row is given by arr[0].length. These expressions are commonly used as the upper bounds in nested loops that traverse the array.

Traversing a 2D Array with Nested Loops

The standard pattern for visiting every element of a 2D array uses two nested for loops — the outer loop iterates over rows and the inner loop iterates over columns: for (int i = 0; i < arr.length; i++) and for (int j = 0; j < arr[0].length; j++). The condition must use strictly less than (<), not less-than-or-equal (<=), to avoid going out of bounds.

Tips for AP-CSA Exam

In the AP Computer Science A (AP CSA) exam, questions on 2D arrays test the ability to read initializer lists to determine the number of rows and columns, apply zero-based indexing to locate specific elements, identify off-by-one errors in loop bounds, and trace nested loops that compute row sums, fill arrays conditionally, or find maximum/minimum values across rows or columns.


Q1. How many rows and columns are there in the integer array initialized below:


    int[][] intArr = { {1, 2}, {8, 12}, {6, 5 }};


(A)	Rows: 2; Columns: 6

(B)	Rows: 3; Columns: 2

(C)	Rows: 6; Columns: 2

(D)	Rows: 3; Columns: 6

(E)	Incorrect way of defining  arrays.

Answer:

View Output

(B)	Rows: 3; Columns: 2

Explanation:

The given array intArr consists of three rows. This evident from the declaration since there are three sets of values enclosed in the outer curly brackets { }. Moreover, there are two columns since, in each set there are two values.



Q2. Consider the declaration and initialization of a two-dimensional array consisting of characters, as given below:


	char[][] charArr = { {'K', 'J'}, {'L', 'I'}, {'T', 'G' }};
    

Which one of the following statements can be used to print the character 'I' from the array?

(A)	System.out.println(charArr[2][2]);

(B)	System.out.println(charArr[2][1]);

(C)	System.out.println(charArr[1][2]);

(D)	System.out.println(charArr[1][1]);

(E)	System.out.println(charArr[0][1]);

Answer:

View Output

(D)	System.out.println(charArr[1][1]);

Explanation:

The given character array charArr has 3 rows and 2 columns. The required character to be printed ('I') is in the second row and second column of the array. However, in arrays, for the purpose of programming, the row as well as column counting starts from 0 (and not from 1). Hence, the array subscripts of access the required value ('I') will be: row = 1 and column =1.



Q3. Which one of the following code segments can be used to find the sum of the diagonal elements of a two-dimensional integer matrix with 3 rows and 3 columns?

(A)	int sum =0;
	for (int i=1; i <= intArr.length; i++)
	{
		for (int j=1; j<= intArr[0].length; j++)
		{
			if (i==j) sum = intArr[j][j] + sum;
		}
	}

(B)	int sum =0;
	for (int i=0; i <= intArr.length; i++)
	{
		for (int j=0; j<= intArr[0].length; j++)
		{
			if (i==j) sum = intArr[i][i] + sum;
		}
	}

(C)	int sum =0;
	for (int i=0; i < intArr.length; i++)
	{
		for (int j=0; j< intArr[0].length; j++)
		{
			if (i==j) sum = intArr[i][j] + sum;
		}
	}

(D)	int sum =0;
	for (int i=1; i < intArr.length; i++)
	{
		for (int j=1; j< intArr[0].length; j++)
		{
			if (i==j) sum = intArr[i][j] + sum;
		}
	}

(E)	int sum =0;
	for (int i=0; i < intArr.length; i++)
	{
		for (int j=0; j< intArr[0].length; j++)
		{
			if (i!=j) sum = intArr[i][j] + sum;
		}
	}

Answer:

View Output

(C)	int sum =0;
	for (int i=0; i < intArr.length; i++)
	{
		for (int j=0; j< intArr[0].length; j++)
		{
			if (i==j) sum = intArr[i][j] + sum;
		}
	}

Explanation:

In a matrix, the diagonal elements have the same subscript. For example, in this case, the elements at locations [0,0], [1,1] and [2,2] are diagonal elements. Hence, we need to look for the case where i and j are equal. This eliminates option (E). Moreover, options (A), (B), and (D) are incorrect because one or more of the loop variables is running beyond the allowed boundaries of the array subscripts.



Q4. Given the following code segment, what is the values of the sum variable after the execution of the first for-loop and after the execution of the second for-loop?


    int[][] intMatrix = { {1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}};
	int sum = 0;
	int firstRow=1;
	int lastRow = intMatrix.length -1;
		
	for (int col = 0; col < intMatrix[0].length; col++)
		{
		   sum = sum + intMatrix[firstRow][col];
		}
	System.out.println(sum);
		
	for (int col = 0; col < intMatrix[0].length; col++)
		{
		   sum = sum + intMatrix[lastRow][col];
		}
	System.out.println(sum);


(A)	4 and 8

(B)	2 and 6

(C)	4 and 6

(D)	2 and 8

(E)	The code will give exception at runtime: ArrayIndexOutOfBoundsException

Answer:

View Output

(B)	2 and 6

Explanation:

Since the variable firstRow is initialised to 1, the first for-loop calculates the sum of all the elements of the second row of the array ({1,0,0,1}), which equals 2 (and not 4). This value of sum is carried over to the next for-loop. Additionally, the second for-loop calculates the sum of all the elements in the last row of the given array ({1,1,1,1}); the sum of element of this last row equals 4; this adds to the previous sum value and we have the final sum value to be 6.




Q5. What will be the contents of intMatrix after the following code segment has been executed?


    int [][] intMatrix = new int [4][4];
	for (int row = 0; row < intMatrix.length; row++) 
		{
		   for (int col = 0; col < intMatrix[0].length; col++) 
		   {
			   if ((row == intMatrix.length -1) || (col == intMatrix[0].length -1))
		    	  intMatrix[row][col] = 1;
		      else
		    	  intMatrix[row][col] = 0; 
			} 
		}

(A)	{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}

(B)	{{1, 1, 1, 1}, {0, 1, 0, 0}, {0, 0, 1, 0}, {1, 1, 1, 1}}

(C)	{{1, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {1, 1, 1, 1}}

(D)	{{1, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}}

(E)	{{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {1, 1, 1, 1}}

Answer:

View Output

(E)	{{0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {1, 1, 1, 1}}

Explanation:

The if statement in the above code can be simplified to:

if ((row == 3) || (col == 3))

Hence, the code sets all the values in the last row (for all column values) and last column (for all row values) of the matrix to 1 and sets all other values to 0.



Q6. What will be the contents of charMatrix after the following code segment has been executed?


    char [][] charMatrix = new char [4][4];
	for (int row = 0; row < charMatrix.length; row++) 
		{
		   for (int col = 0; col < charMatrix[0].length; col++) 
		   {
			   if ((row == charMatrix.length -1) && (col == charMatrix[0].length -1))
				   charMatrix[row][col] = '*';
		      else
		    	  charMatrix[row][col] = '-'; 
			} 
		}

(A)	{{*, *, *, *}, {-, -, -, -}, {-, -, -, -}, {*, *, *, *}}

(B)	{{-, -, -, -}, {-, -, -, -}, {-, -, -, -}, {*, *, *, *}}

(C)	{{-, -, -, -}, {-, -, -, -}, {-, -, -, -}, {-, -, -, -}}

(D)	{{-, -, -, -}, {-, -, -, -}, {-, -, -, -}, {-, -, -, *}}

(E)	{{-, -, -, *}, {-, -, -, *}, {-, -, -, *}, {*, *, *, *}}

Answer:

View Output

(D)	{{-, -, -, -}, {-, -, -, -}, {-, -, -, -}, {-, -, -, *}}

Explanation:

The if statement in the above code can be simplified to:

if ((row == 3) && (col == 3))

This statement will hold true only for one value in the array which is the element at the last row and last column (charMatrix[3][3]).



Q7. What will be the output after the following code segment has been executed?


    int[][] intArr = { {1, 12, 45}, {80, 1, 55}, {61, 15, 1}};
	int sum = 0;
	int max = Integer.MIN_VALUE;
	for (int i=0; i < intArr.length; i++)
		{
			sum=0;
			for (int j=0; j< intArr[0].length; j++)
			{
				sum = sum + intArr[i][j];
			}
			if (max < sum) max = sum;
		}
	System.out.println("Max is: "+max);

(A)	Max is: 271

(B)	Max is: 136

(C)	Max is: 77

(D)	Max is: 58

(E)	Max is: 101

Answer:

View Output

Max is: 136

Explanation:

The code aims to find the value of the max row-sum for the given matrix, which is 136.

i j sum max i++; j++;
For-loop: First
execution
0 0 sum = 0 + 1 = 1 Integer.MIN_VALUE; 1
1 sum = 1 + 12 = 13 Integer.MIN_VALUE; 2
2 sum = 13 + 45 = 58 Integer.MIN_VALUE; 3
if (max < sum)
max = sum;
max = 58
For-loop: Second
execution
1 0 sum = 0 + 80 = 80 max = 58 1
1 sum = 80 + 1 = 81 max = 58 2
2 sum = 81 + 55 = 136 max = 58 3
if (max < sum)
max = sum;
max = 136
For-loop: Third
execution
2 0 sum = 0 + 61 = 61 max = 136 1
1 sum = 61 + 15 = 76 max = 136 2
2 sum = 76 + 1 = 77 max = 136 3
if (max < sum)
max = sum;
max = 136


<-- Back to Using Loops Next to ArrayLists-->