Challenge - 5 Problems
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of array sum calculation
What is the output of this Java code that sums all elements in an integer array?
Java
int[] numbers = {2, 4, 6, 8}; int sum = 0; for (int num : numbers) { sum += num; } System.out.println(sum);
Attempts:
2 left
💡 Hint
Add each element of the array to the sum variable inside the loop.
✗ Incorrect
The code adds 2 + 4 + 6 + 8 which equals 20, so the output is 20.
❓ Predict Output
intermediateOutput of array reverse print
What does this Java code print when it prints the array elements in reverse order?
Java
int[] arr = {1, 3, 5, 7}; for (int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i] + " "); }
Attempts:
2 left
💡 Hint
The loop starts from the last index and goes down to zero.
✗ Incorrect
The loop prints elements starting from index 3 to 0, so output is '7 5 3 1 '.
❓ Predict Output
advancedOutput of array element replacement
What is the output after replacing all negative numbers with zero in this array?
Java
int[] data = {4, -1, 7, -3, 0}; for (int i = 0; i < data.length; i++) { if (data[i] < 0) { data[i] = 0; } } for (int num : data) { System.out.print(num + " "); }
Attempts:
2 left
💡 Hint
Check each element and replace if it is less than zero.
✗ Incorrect
Negative values -1 and -3 are replaced with 0, so output is '4 0 7 0 0 '.
❓ Predict Output
advancedOutput of array copy with System.arraycopy
What is the output of this code that copies part of one array into another?
Java
int[] source = {10, 20, 30, 40, 50}; int[] dest = {1, 2, 3, 4, 5}; System.arraycopy(source, 1, dest, 2, 2); for (int i : dest) { System.out.print(i + " "); }
Attempts:
2 left
💡 Hint
System.arraycopy copies 2 elements starting at index 1 from source to index 2 in dest.
✗ Incorrect
Elements 20 and 30 from source replace dest[2] and dest[3], so output is '1 2 20 30 5 '.
🧠 Conceptual
expertError type when accessing invalid array index
What error does this Java code produce when trying to access an invalid index in an array?
Java
int[] arr = {5, 10, 15}; System.out.println(arr[5]);
Attempts:
2 left
💡 Hint
Accessing an index outside the array length causes a runtime exception.
✗ Incorrect
Index 5 is outside the array bounds (length 3), so Java throws ArrayIndexOutOfBoundsException.
