Challenge - 5 Problems
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of array initialization and access
What is the output of the following Java code?
Java
public class Main { public static void main(String[] args) { int[] numbers = new int[3]; numbers[0] = 5; numbers[1] = 10; numbers[2] = 15; System.out.println(numbers[1]); } }
Attempts:
2 left
💡 Hint
Remember array indices start at 0.
✗ Incorrect
The array 'numbers' has elements 5, 10, and 15 at indices 0, 1, and 2 respectively. Accessing numbers[1] prints 10.
❓ Predict Output
intermediateOutput of array initialization with values
What will be printed by this Java program?
Java
public class Main { public static void main(String[] args) { String[] fruits = {"Apple", "Banana", "Cherry"}; System.out.println(fruits.length); } }
Attempts:
2 left
💡 Hint
The length property tells how many items are in the array.
✗ Incorrect
The array 'fruits' has three elements, so fruits.length is 3.
❓ Predict Output
advancedOutput of multi-dimensional array initialization
What is the output of this Java code?
Java
public class Main { public static void main(String[] args) { int[][] matrix = new int[2][3]; matrix[0][1] = 7; matrix[1][2] = 9; System.out.println(matrix[1][2] + matrix[0][1]); } }
Attempts:
2 left
💡 Hint
Add the two assigned values from the matrix.
✗ Incorrect
matrix[0][1] is 7 and matrix[1][2] is 9, their sum is 16.
❓ Predict Output
advancedOutput of array default values
What will this Java program print?
Java
public class Main { public static void main(String[] args) { boolean[] flags = new boolean[4]; System.out.println(flags[2]); } }
Attempts:
2 left
💡 Hint
Arrays of booleans are initialized with false by default.
✗ Incorrect
Boolean arrays in Java default to false for all elements.
🧠 Conceptual
expertNumber of elements in a jagged array
Consider the following Java code snippet. How many elements does the jagged array contain in total?
Java
public class Main { public static void main(String[] args) { int[][] jagged = new int[3][]; jagged[0] = new int[2]; jagged[1] = new int[4]; jagged[2] = new int[3]; } }
Attempts:
2 left
💡 Hint
Add the lengths of each inner array.
✗ Incorrect
The jagged array has inner arrays of lengths 2, 4, and 3. Total elements = 2 + 4 + 3 = 9.
