Challenge - 5 Problems
2D Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate2: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(); } } }
Attempts:
2 left
💻 code output
intermediate1: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]?
Attempts:
2 left
🔧 debug
advanced1: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;
Attempts:
2 left
🧠 conceptual
advanced1: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} };
Attempts:
2 left
💻 code output
expert2: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(); } } }
Attempts:
2 left
