Java Arrays 2D Practice
Two-dimensional arrays and matrices: sums, diagonals, transpose and multiplication.
1
Sum of Matrix Elements
Easy
Read R, C and then R*C integers (row-major). Print the sum of all elements.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the sum.
Sample Test Cases
|
Sample Input 1
2 2 1 2 3 4 |
Sample Output 1
10 |
|
Sample Input 2
2 3 1 2 3 4 5 6 |
Sample Output 2
21 |
|
Sample Input 3
1 1 7 |
Sample Output 3
7 |
|
Sample Input 4
3 3 1 1 1 1 1 1 1 1 1 |
Sample Output 4
9 |
|
Sample Input 5
2 2 -1 2 3 -4 |
Sample Output 5
0 |
Constraints
1
Explanation
Nested loops accumulating sum.
2
Largest Matrix Element
Easy
Read R, C and then R*C integers. Print the largest element.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the maximum.
Sample Test Cases
|
Sample Input 1
2 2 1 2 3 4 |
Sample Output 1
4 |
|
Sample Input 2
2 3 9 2 3 4 5 6 |
Sample Output 2
9 |
|
Sample Input 3
1 1 7 |
Sample Output 3
7 |
|
Sample Input 4
2 2 -1 -2 -3 -4 |
Sample Output 4
-1 |
|
Sample Input 5
3 2 1 2 3 4 5 6 |
Sample Output 5
6 |
Constraints
1
Explanation
Track a running maximum while reading.
3
Row Sums
Easy
Read R, C and then R*C integers. Print the sum of each row, one per line.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
R lines: each row sum.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
6 15 |
|
Sample Input 2
3 2 1 1 2 2 3 3 |
Sample Output 2
2 4 6 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
6 |
|
Sample Input 4
2 2 -1 2 3 -4 |
Sample Output 4
1 -1 |
|
Sample Input 5
3 3 1 1 1 1 1 1 1 1 1 |
Sample Output 5
3 3 3 |
Constraints
1
Explanation
Sum each row and print.
4
Column Sums
Easy
Read R, C and then R*C integers. Print the sum of each column, one per line.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
C lines: each column sum.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
5 7 9 |
|
Sample Input 2
3 2 1 1 2 2 3 3 |
Sample Output 2
6 6 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
1 2 3 |
|
Sample Input 4
2 2 -1 2 3 -4 |
Sample Output 4
2 -2 |
|
Sample Input 5
2 2 1 1 1 1 |
Sample Output 5
2 2 |
Constraints
1
Explanation
Sum each column and print.
5
Count Odd Numbers in Matrix
Easy
Read R, C and then R*C integers. Print how many are odd.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the count of odds.
Sample Test Cases
|
Sample Input 1
2 2 1 2 3 4 |
Sample Output 1
2 |
|
Sample Input 2
2 3 1 2 3 4 5 6 |
Sample Output 2
3 |
|
Sample Input 3
1 1 7 |
Sample Output 3
1 |
|
Sample Input 4
2 2 2 4 6 8 |
Sample Output 4
0 |
|
Sample Input 5
3 3 1 1 1 1 1 1 1 1 1 |
Sample Output 5
9 |
Constraints
1
Explanation
Count m[i][j] % 2 != 0.
1
Transpose Matrix
Medium
Read R, C and then R*C integers. Print the transpose (C rows, R columns).
Input Format
First line: R C. Next R lines: C integers each.
Output Format
C lines: the transposed matrix.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
1 4 2 5 3 6 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
1 3 2 4 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
1 2 3 |
|
Sample Input 4
3 1 1 2 3 |
Sample Output 4
1 2 3 |
|
Sample Input 5
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 5
1 4 7 2 5 8 3 6 9 |
Constraints
1
Explanation
Print m[j][i] for each column j.
2
Main Diagonal Sum
Medium
Read R, C and then R*C integers. Print the sum of the main diagonal (top-left to bottom-right). R == C.
Input Format
First line: R C (equal). Next R lines: C integers each.
Output Format
A single integer: the diagonal sum.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
15 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
5 |
|
Sample Input 3
1 1 7 |
Sample Output 3
7 |
|
Sample Input 4
3 3 1 0 0 0 1 0 0 0 1 |
Sample Output 4
3 |
|
Sample Input 5
2 2 -1 2 3 -4 |
Sample Output 5
-5 |
Constraints
1
Explanation
sum += m[i][i].
3
Secondary Diagonal Sum
Medium
Read R, C and then R*C integers. Print the sum of the secondary diagonal (top-right to bottom-left). R == C.
Input Format
First line: R C (equal). Next R lines: C integers each.
Output Format
A single integer: the secondary diagonal sum.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
15 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
5 |
|
Sample Input 3
1 1 7 |
Sample Output 3
7 |
|
Sample Input 4
3 3 1 0 0 0 1 0 0 0 1 |
Sample Output 4
1 |
|
Sample Input 5
2 2 -1 2 3 -4 |
Sample Output 5
5 |
Constraints
1
Explanation
For row i, the secondary diagonal element is m[i][C-1-i].
4
Matrix Equality
Medium
Read R, C and then R*C integers for matrix A, then R*C integers for matrix B. Print "Equal" if identical, otherwise "Not Equal".
Input Format
First line: R C. Next R lines: A. Next R lines: B.
Output Format
"Equal" or "Not Equal".
Sample Test Cases
|
Sample Input 1
2 2 1 2 3 4 1 2 3 4 |
Sample Output 1
Equal |
|
Sample Input 2
2 2 1 2 3 4 1 2 3 5 |
Sample Output 2
Not Equal |
|
Sample Input 3
1 1 7 7 |
Sample Output 3
Equal |
|
Sample Input 4
2 2 1 1 1 1 1 1 1 1 |
Sample Output 4
Equal |
|
Sample Input 5
3 3 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 |
Sample Output 5
Equal |
Constraints
1
Explanation
Compare element by element.
5
Boundary Sum
Medium
Read R, C and then R*C integers. Print the sum of the boundary elements (first/last row and first/last column). Count corner elements once.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the boundary sum.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
40 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
10 |
|
Sample Input 3
1 1 7 |
Sample Output 3
7 |
|
Sample Input 4
3 3 1 1 1 1 0 1 1 1 1 |
Sample Output 4
8 |
|
Sample Input 5
3 4 1 2 3 4 5 6 7 8 9 10 11 12 |
Sample Output 5
65 |
Constraints
1
Explanation
Sum elements where i==0 || i==R-1 || j==0 || j==C-1.
6
Row with Maximum Sum
Medium
Read R, C and then R*C integers. Print the index (0-based) of the row with the maximum sum. If tied, print the first such row.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the row index.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
2 |
|
Sample Input 2
2 3 10 20 30 1 2 3 |
Sample Output 2
0 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
0 |
|
Sample Input 4
3 2 1 1 1 1 2 2 |
Sample Output 4
2 |
|
Sample Input 5
4 2 1 2 3 4 5 6 7 8 |
Sample Output 5
3 |
Constraints
1
Explanation
Compute each row sum and track the max.
7
Element at Position
Medium
Read R, C and then R*C integers, then two indices I and J (0-based). Print the element at row I, column J.
Input Format
First line: R C. Next R lines: C integers each. Last line: I J.
Output Format
A single integer: m[I][J].
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 1 1 |
Sample Output 1
5 |
|
Sample Input 2
2 2 1 2 3 4 0 1 |
Sample Output 2
2 |
|
Sample Input 3
1 1 7 0 0 |
Sample Output 3
7 |
|
Sample Input 4
3 3 1 2 3 4 5 6 7 8 9 2 0 |
Sample Output 4
7 |
|
Sample Input 5
2 3 1 2 3 4 5 6 1 2 |
Sample Output 5
6 |
Constraints
1
Explanation
Index into the matrix after reading.
1
Matrix Addition
Hard
Read R, C and then R*C integers for A, then R*C for B. Print the matrix A + B.
Input Format
First line: R C. Next R lines: A. Next R lines: B.
Output Format
R lines: each element A[i][j] + B[i][j].
Sample Test Cases
|
Sample Input 1
2 2 1 2 3 4 5 6 7 8 |
Sample Output 1
6 8 10 12 |
|
Sample Input 2
2 3 1 1 1 1 1 1 2 2 2 2 2 2 |
Sample Output 2
3 3 3 3 3 3 |
|
Sample Input 3
1 1 5 7 |
Sample Output 3
12 |
|
Sample Input 4
3 3 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 |
Sample Output 4
2 0 0 0 2 0 0 0 2 |
|
Sample Input 5
2 2 -1 2 3 -4 4 -2 -3 1 |
Sample Output 5
3 0 0 -3 |
Constraints
1
Explanation
Add element by element.
2
Identity Matrix Check
Hard
Read R, C and then R*C integers. Print "Identity" if the matrix is an identity matrix (1 on the main diagonal, 0 elsewhere), otherwise "Not Identity". R == C.
Input Format
First line: R C (equal). Next R lines: C integers each.
Output Format
"Identity" or "Not Identity".
Sample Test Cases
|
Sample Input 1
3 3 1 0 0 0 1 0 0 0 1 |
Sample Output 1
Identity |
|
Sample Input 2
2 2 1 0 0 0 |
Sample Output 2
Not Identity |
|
Sample Input 3
1 1 1 |
Sample Output 3
Identity |
|
Sample Input 4
3 3 1 0 0 0 1 0 0 0 2 |
Sample Output 4
Not Identity |
|
Sample Input 5
2 2 1 1 0 1 |
Sample Output 5
Not Identity |
Constraints
1
Explanation
Check m[i][i] == 1 and m[i][j] == 0 for i != j.
3
Matrix Multiplication
Hard
Read R, K, C, then R*K integers for A, then K*C integers for B. Print the product matrix A * B (R rows, C columns).
Input Format
First line: R K C. Next R lines: A (K each). Next K lines: B (C each).
Output Format
R lines: the product matrix.
Sample Test Cases
|
Sample Input 1
2 2 2 1 2 3 4 5 6 7 8 |
Sample Output 1
19 22 43 50 |
|
Sample Input 2
1 2 1 1 2 3 4 |
Sample Output 2
11 |
|
Sample Input 3
2 1 2 1 2 3 4 |
Sample Output 3
3 4 6 8 |
|
Sample Input 4
3 2 2 1 2 3 4 5 6 1 0 0 1 |
Sample Output 4
1 2 3 4 5 6 |
|
Sample Input 5
1 1 1 3 4 |
Sample Output 5
12 |
Constraints
1
Explanation
Three nested loops for matrix multiplication.
4
Row Reverse
Hard
Read R, C and then R*C integers. Print the matrix with each row reversed.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
R lines: each row reversed.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
3 2 1 6 5 4 |
|
Sample Input 2
3 2 1 2 3 4 5 6 |
Sample Output 2
2 1 4 3 6 5 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
3 2 1 |
|
Sample Input 4
2 2 -1 2 3 -4 |
Sample Output 4
2 -1 -4 3 |
|
Sample Input 5
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 5
3 2 1 6 5 4 9 8 7 |
Constraints
1
Explanation
Print each row from last column to first.
5
Count Zeros in Matrix
Hard
Read R, C and then R*C integers. Print how many elements are zero.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the zero count.
Sample Test Cases
|
Sample Input 1
2 2 1 0 0 4 |
Sample Output 1
2 |
|
Sample Input 2
2 3 0 1 2 0 0 3 |
Sample Output 2
3 |
|
Sample Input 3
1 1 0 |
Sample Output 3
1 |
|
Sample Input 4
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 4
0 |
|
Sample Input 5
2 2 0 0 0 0 |
Sample Output 5
4 |
Constraints
1
Explanation
Count m[i][j] == 0.
6
Column with Maximum Sum
Hard
Read R, C and then R*C integers. Print the index (0-based) of the column with the maximum sum. If tied, print the first such column.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
A single integer: the column index.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
2 |
|
Sample Input 2
2 3 10 1 1 20 1 1 |
Sample Output 2
0 |
|
Sample Input 3
3 1 1 2 3 |
Sample Output 3
0 |
|
Sample Input 4
2 2 1 2 3 4 |
Sample Output 4
1 |
|
Sample Input 5
4 2 1 1 1 1 1 1 1 1 |
Sample Output 5
0 |
Constraints
1
Explanation
Compute each column sum and track the max.
7
Matrix Spiral Print
Hard
Read R, C and then R*C integers. Print the matrix elements in spiral order (top row, right column, bottom row, left column, ...) on one line, space-separated.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
The spiral traversal on one line.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 4 5 6 7 8 9 |
Sample Output 1
1 2 3 6 9 8 7 4 5 |
|
Sample Input 2
2 2 1 2 3 4 |
Sample Output 2
1 2 4 3 |
|
Sample Input 3
1 3 1 2 3 |
Sample Output 3
1 2 3 |
|
Sample Input 4
3 1 1 2 3 |
Sample Output 4
1 2 3 |
|
Sample Input 5
3 4 1 2 3 4 5 6 7 8 9 10 11 12 |
Sample Output 5
1 2 3 4 8 12 11 10 9 5 6 7 |
Constraints
1
Explanation
Shrink the boundaries top/bottom/left/right in a loop.
8
Matrix Diagonal Mirror
Hard
Read R, C and then R*C integers. Print the matrix transposed (mirror across the main diagonal). Same as transpose; R and C may differ.
Input Format
First line: R C. Next R lines: C integers each.
Output Format
C lines: the transposed matrix.
Sample Test Cases
|
Sample Input 1
2 3 1 2 3 4 5 6 |
Sample Output 1
1 4 2 5 3 6 |
|
Sample Input 2
3 2 1 2 3 4 5 6 |
Sample Output 2
1 3 5 2 4 6 |
|
Sample Input 3
1 1 7 |
Sample Output 3
7 |
|
Sample Input 4
2 2 1 2 3 4 |
Sample Output 4
1 3 2 4 |
|
Sample Input 5
1 4 1 2 3 4 |
Sample Output 5
1 2 3 4 |
Constraints
1
Explanation
Print m[j][i].
Competitive MCQs — Java Arrays 2D
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 32
Q1
How do you declare a 2D array with 3 rows and 4 columns?
Correct!
Wrong — correct answer is .
Two pairs of brackets: int[][] m = new int[3][4].
Q2
What is the output of this code?
java
1
2
2
int[][] m = {{1, 2}, {3, 4}};
System.out.println(m[1][0]);Correct!
Wrong — correct answer is .
Row 1, column 0 is the third element, 3.
Q3
What is the output of this code?
int[][] m = {{1, 2}, {3, 4}};
System.out.println(m[1][0]);
int[][] m = {{1, 2}, {3, 4}};
System.out.println(m[1][0]);
Correct!
Wrong — correct answer is .
Row 1, column 0 is the third element, 3.
Q4
Which expression gives the number of rows in int[][] m?
Correct!
Wrong — correct answer is .
m.length is the row count; m[0].length is the column count.
Q5
What is the output of this code?
java
1
2
2
int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[0].length);Correct!
Wrong — correct answer is .
m[0] has 3 elements.
Q6
What is the output of this code?
int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[0].length);
int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[0].length);
Correct!
Wrong — correct answer is .
m[0] has 3 elements.
Q7
Which loop visits every element of int[][] grid?
Correct!
Wrong — correct answer is .
2D traversal needs nested loops.
Q8
What is the output of this code?
java
1
2
3
4
5
2
3
4
5
int[][] m = {{1, 2}, {3, 4}};
int s = 0;
for (int[] row : m)
for (int v : row) s += v;
System.out.println(s);Correct!
Wrong — correct answer is .
Sums all four elements: 1+2+3+4 = 10.
Q9
What is the output of this code?
int[][] m = {{1, 2}, {3, 4}};
int s = 0;
for (int[] row : m)
for (int v : row) s += v;
System.out.println(s);
int[][] m = {{1, 2}, {3, 4}};
int s = 0;
for (int[] row : m)
for (int v : row) s += v;
System.out.println(s);
Correct!
Wrong — correct answer is .
Sums all four elements: 1+2+3+4 = 10.
Q10
What is a jagged array?
Correct!
Wrong — correct answer is .
In Java, each row may be a different length.
Q11
What is the output of this code?
java
1
2
2
int[][] m = {{1, 2}, {3, 4}};
System.out.println(m[1][1]);Correct!
Wrong — correct answer is .
Row 1, column 1 is 4.
Q12
What is the output of this code?
int[][] m = {{1, 2}, {3, 4}};
System.out.println(m[1][1]);
int[][] m = {{1, 2}, {3, 4}};
System.out.println(m[1][1]);
Correct!
Wrong — correct answer is .
Row 1, column 1 is 4.
Q13
Which initializer creates a 2x2 identity-like matrix?
Correct!
Wrong — correct answer is .
Curly braces with rows as inner arrays is correct syntax.
Q14
What is the output of this code?
java
1
2
2
int[][] m = new int[2][2];
System.out.println(m[0][1]);Correct!
Wrong — correct answer is .
All int cells default to 0.
Q15
What is the output of this code?
int[][] m = new int[2][2];
System.out.println(m[0][1]);
int[][] m = new int[2][2];
System.out.println(m[0][1]);
Correct!
Wrong — correct answer is .
All int cells default to 0.
Q16
What is the output of this code?
int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[1][2]);
int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[1][2]);
Correct!
Wrong — correct answer is .
Row 1, column 2 is 6.
Q17
Which gives the number of rows in int[][] m?
Correct!
Wrong — correct answer is .
m.length is the row count; each row is an array.
Q18
Which gives the number of columns in a rectangular 2D array m?
Correct!
Wrong — correct answer is .
m[0].length is the length of the first row, i.e., the column count.
Q19
What is the output of this code?
int[][] m = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(m.length);
int[][] m = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(m.length);
Correct!
Wrong — correct answer is .
There are three rows.
Q20
What is the output of this code?
int[][] m = {{1}, {2, 3}, {4, 5, 6}};
System.out.println(m[2].length);
int[][] m = {{1}, {2, 3}, {4, 5, 6}};
System.out.println(m[2].length);
Correct!
Wrong — correct answer is .
Row 2 holds three elements.
Q21
Which loops visit every element of int[][] grid?
Correct!
Wrong — correct answer is .
2D traversal needs an outer loop over rows and inner over columns.
Q22
How do you create a 3-row jagged array where each row is allocated later?
Correct!
Wrong — correct answer is .
Declare the row count and assign each row its own array.
Q23
What is the output of this code?
int[][] m = {{5, 6}, {7, 8}};
System.out.println(m[0][1] + m[1][0]);
int[][] m = {{5, 6}, {7, 8}};
System.out.println(m[0][1] + m[1][0]);
Correct!
Wrong — correct answer is .
6 + 7 = 13.
Q24
Which initializes a 2×3 matrix with zeros?
Correct!
Wrong — correct answer is .
new int[2][3] gives 2 rows and 3 columns of zeros.
Q25
What happens when you access m[2] in a 2-row array?
Correct!
Wrong — correct answer is .
Row index 2 is beyond the last row index 1.
Q26
Which of these prints the diagonal of a square matrix m?
Correct!
Wrong — correct answer is .
The diagonal uses equal row and column indexes.
Q27
What is the output of this code?
int[][] m = new int[2][2];
m[1][1] = 9;
System.out.println(m[1][1]);
int[][] m = new int[2][2];
m[1][1] = 9;
System.out.println(m[1][1]);
Correct!
Wrong — correct answer is .
Assignment stores 9 at row 1, column 1.
Q28
What does int[][] mean?
Correct!
Wrong — correct answer is .
int[][] is an array whose elements are themselves int arrays.
Q29
What is the output of this code?
int[][] m = {{1}, {1, 2}};
System.out.println(m[1][1]);
int[][] m = {{1}, {1, 2}};
System.out.println(m[1][1]);
Correct!
Wrong — correct answer is .
Row 1 has two elements; index 1 is 2.
Q30
Which statement is TRUE about Java 2D arrays?
Correct!
Wrong — correct answer is .
Each row is a separate array object, enabling jagged shapes.
Q31
What is the output of this code?
int[][] m = {{1, 2}, {3, 4}};
int total = 0;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
total += m[i][j];
System.out.println(total);
int[][] m = {{1, 2}, {3, 4}};
int total = 0;
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[i].length; j++)
total += m[i][j];
System.out.println(total);
Correct!
Wrong — correct answer is .
Indexed nested loops add all four cells: 1+2+3+4 = 10.
Q32
What is the output of this code?
int[][] m = {{1, 1}, {1, 1}};
System.out.println(m[0][0] + m[1][1]);
int[][] m = {{1, 1}, {1, 1}};
System.out.println(m[0][0] + m[1][1]);
Correct!
Wrong — correct answer is .
Both diagonal entries are 1, giving 2.