Challenge - 5 Problems
Enhanced For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enhanced for loop with array
What is the output of the following Java code using an enhanced for loop?
Java
public class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3}; int sum = 0; for (int num : numbers) { sum += num; } System.out.println(sum); } }
Attempts:
2 left
💡 Hint
Think about how the enhanced for loop adds each number to sum.
✗ Incorrect
The enhanced for loop iterates over each element in the array 'numbers'. It adds each element to 'sum'. The sum of 1 + 2 + 3 is 6, so the output is 6.
❓ Predict Output
intermediate2:00remaining
Enhanced for loop with a list
What will be printed by this Java code using an enhanced for loop over a List?
Java
import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("apple"); fruits.add("banana"); fruits.add("cherry"); for (String fruit : fruits) { System.out.print(fruit.charAt(0)); } } }
Attempts:
2 left
💡 Hint
Look at what character is printed for each fruit.
✗ Incorrect
The enhanced for loop goes through each fruit in the list. For each fruit, it prints the first character (charAt(0)). So it prints 'a' from apple, 'b' from banana, and 'c' from cherry, resulting in 'abc'.
❓ Predict Output
advanced2:00remaining
Enhanced for loop with modification inside loop
What is the output of this Java code that tries to modify array elements inside an enhanced for loop?
Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3}; for (int x : arr) { x = x * 2; } for (int x : arr) { System.out.print(x + " "); } } }
Attempts:
2 left
💡 Hint
Does changing 'x' inside the loop change the original array?
✗ Incorrect
The enhanced for loop variable 'x' is a copy of each element. Changing 'x' does not change the original array elements. So the array remains {1, 2, 3}, and the second loop prints '1 2 3 '.
❓ Predict Output
advanced2:00remaining
Enhanced for loop with multidimensional array
What will this Java program print when using an enhanced for loop over a 2D array?
Java
public class Main { public static void main(String[] args) { int[][] matrix = {{1, 2}, {3, 4}}; int total = 0; for (int[] row : matrix) { for (int val : row) { total += val; } } System.out.println(total); } }
Attempts:
2 left
💡 Hint
Add all numbers in the 2D array.
✗ Incorrect
The outer enhanced for loop iterates over each row (which is an int array). The inner loop iterates over each value in that row. Adding 1 + 2 + 3 + 4 equals 10, so the output is 10.
🧠 Conceptual
expert2:00remaining
Behavior of enhanced for loop with Iterable and Iterator
Which statement about the enhanced for loop in Java is true?
Attempts:
2 left
💡 Hint
Think about how enhanced for loops work with collections like ArrayList.
✗ Incorrect
The enhanced for loop works with arrays and any class that implements Iterable. Internally, it uses an Iterator to get elements one by one. It does not allow modifying the collection elements directly, and it does not require manual index handling.