Challenge - 5 Problems
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of array element modification
What is the output of this Java code snippet?
Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; arr[2] = arr[0] + arr[4]; System.out.println(arr[2]); } }
Attempts:
2 left
💡 Hint
Remember array indices start at 0. arr[2] is changed to arr[0] + arr[4].
✗ Incorrect
arr[0] is 1 and arr[4] is 5, so arr[2] becomes 1 + 5 = 6.
❓ Predict Output
intermediateSum of array elements
What is the output of this Java program?
Java
public class Main { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40}; int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } System.out.println(sum); } }
Attempts:
2 left
💡 Hint
Add all elements in the array.
✗ Incorrect
Sum is 10 + 20 + 30 + 40 = 100.
🔧 Debug
advancedIdentify the error in array access
What error does this Java code produce when run?
Java
public class Main { public static void main(String[] args) { int[] arr = new int[3]; arr[3] = 10; System.out.println(arr[3]); } }
Attempts:
2 left
💡 Hint
Check the valid indices for an array of size 3.
✗ Incorrect
Array indices go from 0 to length-1. arr[3] is out of bounds.
❓ Predict Output
advancedOutput after array element swap
What is the output of this Java program?
Java
public class Main { public static void main(String[] args) { int[] arr = {7, 8, 9}; int temp = arr[0]; arr[0] = arr[2]; arr[2] = temp; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
Attempts:
2 left
💡 Hint
The first and last elements are swapped.
✗ Incorrect
arr[0] and arr[2] are swapped, so array becomes {9, 8, 7}.
🧠 Conceptual
expertNumber of elements after array initialization
How many elements does the array contain after this declaration?
int[] arr = new int[5];
Attempts:
2 left
💡 Hint
The number inside the brackets defines the size.
✗ Incorrect
The array is created with 5 elements indexed 0 to 4.
