0
0
Javaprogramming~15 mins

Two-dimensional arrays in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
2D Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of nested loop filling 2D array
What is the output of the following Java code that fills a 2D array and prints it?
Java
public class Main {
    public static void main(String[] args) {
        int[][] arr = new int[2][3];
        int count = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = count++;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}
A
0 0 0 
0 0 0 
B
1 3 5 
2 4 6 
C
1 2 3 
4 5 6 
D
1 2 3 4 5 6 
Attempts:
2 left
💻 code output
intermediate
1:30remaining
Value of element after initialization
What is the value of arr[1][2] after this code runs?
Java
int[][] arr = {
    {5, 10, 15},
    {20, 25, 30},
    {35, 40, 45}
};
// What is arr[1][2]?
A15
B25
C40
D30
Attempts:
2 left
🔧 debug
advanced
1:30remaining
Identify the error in 2D array declaration
What error does this code produce?
Java
int[][] arr = new int[2][];
arr[0] = new int[3];
arr[1] = new int[2];
arr[0][3] = 5;
AArrayIndexOutOfBoundsException
BNullPointerException
CSyntaxError
DNo error
Attempts:
2 left
🧠 conceptual
advanced
1:30remaining
Number of elements in jagged 2D array
Given the jagged array below, how many total elements does it contain?
Java
int[][] arr = {
    {1, 2},
    {3, 4, 5},
    {6}
};
A6
B5
C3
D7
Attempts:
2 left
💻 code output
expert
2:30remaining
Output of 2D array transpose code
What is the output of this Java code that transposes a 2D array?
Java
public class Main {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6}
        };
        int rows = matrix.length;
        int cols = matrix[0].length;
        int[][] transpose = new int[cols][rows];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                transpose[j][i] = matrix[i][j];
            }
        }
        for (int i = 0; i < transpose.length; i++) {
            for (int j = 0; j < transpose[i].length; j++) {
                System.out.print(transpose[i][j] + " ");
            }
            System.out.println();
        }
    }
}
A
1 2 3 
4 5 6 
B
1 4 
2 5 
3 6 
C
1 4 2 5 3 6 
D
4 1 
5 2 
6 3 
Attempts:
2 left