Bird
Raised Fist0
Javaprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
2D Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict 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
💡 Hint
Look at how the count variable increments inside the nested loops.
❓ Predict 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
💡 Hint
Remember that the first index is the row, the second is the column.
🔧 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
💡 Hint
Check the valid indices for arr[0].
🧠 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
💡 Hint
Count the elements in each inner array and add them up.
❓ Predict 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
💡 Hint
Transpose swaps rows and columns.

Practice

(1/5)
1. What does a two-dimensional array in Java represent?
easy
A. A collection of unrelated variables
B. A single list of elements
C. A table with rows and columns
D. A method to sort numbers

Solution

  1. Step 1: Understand the structure of two-dimensional arrays

    Two-dimensional arrays store data in rows and columns, like a grid or table.
  2. Step 2: Compare options with the definition

    Only A table with rows and columns correctly describes this as a table with rows and columns.
  3. Final Answer:

    A table with rows and columns -> Option C
  4. Quick Check:

    Two-dimensional array = table [OK]
Hint: Think of it as a spreadsheet with rows and columns [OK]
Common Mistakes:
  • Confusing with one-dimensional arrays
  • Thinking it's a list of unrelated variables
  • Mixing arrays with methods
2. Which of the following is the correct way to declare a two-dimensional array of integers in Java?
easy
A. int matrix[3][4];
B. int[][] matrix = new int[3][4];
C. int matrix = new int[3,4];
D. int[] matrix = new int[3][4];

Solution

  1. Step 1: Recall Java syntax for 2D array declaration

    Java uses int[][] name = new int[rows][columns]; to declare 2D arrays.
  2. Step 2: Check each option

    int[][] matrix = new int[3][4]; matches correct syntax. int matrix[3][4]; is C-style, invalid in Java. int matrix = new int[3,4]; uses comma, invalid. int[] matrix = new int[3][4]; declares a 1D array incorrectly.
  3. Final Answer:

    int[][] matrix = new int[3][4]; -> Option B
  4. Quick Check:

    Correct 2D array syntax = int[][] matrix = new int[3][4]; [OK]
Hint: Use double brackets [][] and new with sizes [OK]
Common Mistakes:
  • Using commas instead of separate brackets
  • Trying C-style declarations
  • Mixing 1D and 2D array syntax
3. What is the output of the following Java code?
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(arr[1][0]);
medium
A. 3
B. 5
C. 4
D. 2

Solution

  1. Step 1: Identify the element at arr[1][0]

    arr[1] is the second row: {3, 4}. The element at index 0 in this row is 3.
  2. Step 2: Confirm the printed value

    System.out.println prints arr[1][0], which is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    arr[1][0] = 3 [OK]
Hint: Remember indexing starts at 0 for rows and columns [OK]
Common Mistakes:
  • Confusing row and column indices
  • Starting count at 1 instead of 0
  • Mixing up elements in nested arrays
4. Find the error in the following code snippet:
int[][] grid = new int[2][3];
grid[2][1] = 5;
medium
A. Array index out of bounds error
B. Syntax error in declaration
C. Missing initialization of array
D. No error, code runs fine

Solution

  1. Step 1: Check array size and indices used

    Array grid has 2 rows (indices 0 and 1) and 3 columns (indices 0 to 2). Accessing grid[2][1] uses row index 2 which is out of bounds.
  2. Step 2: Identify the error type

    Accessing an invalid index causes ArrayIndexOutOfBoundsException at runtime.
  3. Final Answer:

    Array index out of bounds error -> Option A
  4. Quick Check:

    Index 2 invalid for 2-row array [OK]
Hint: Check array size before accessing indices [OK]
Common Mistakes:
  • Using indices equal to array length
  • Confusing rows and columns sizes
  • Assuming arrays auto-expand
5. Given a 2D array int[][] data = {{1,2},{3,4},{5,6}};, which code snippet correctly calculates the sum of all elements?
hard
A. int sum = 0; for(int i=0; i
B. int sum = 0; for(int i=0; i
C. int sum = 0; for(int i=0; i
D. int sum = 0; for(int i=0; i

Solution

  1. Step 1: Understand array dimensions and iteration

    data.length is number of rows (3). data[i].length is number of columns (2). Nested loops must iterate rows then columns.
  2. Step 2: Analyze each option's loop structure

    int sum = 0; for(int i=0; i
  3. Final Answer:

    The nested loops with outer loop on data.length and inner on data[i].length -> Option D
  4. Quick Check:

    Correct nested loops sum all elements [OK]
Hint: Outer loop rows, inner loop columns for 2D arrays [OK]
Common Mistakes:
  • Swapping row and column loops
  • Adding arrays directly to int
  • Using wrong indices in loops
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard