0
0
Javaprogramming~20 mins

Enhanced for loop in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enhanced For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ACompilation error
B123
C0
D6
Attempts:
2 left
💡 Hint
Think about how the enhanced for loop adds each number to sum.
Predict Output
intermediate
2: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));
        }
    }
}
Aapplebananacherry
Ba b c
Cabc
DCompilation error
Attempts:
2 left
💡 Hint
Look at what character is printed for each fruit.
Predict Output
advanced
2: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 + " ");
        }
    }
}
A1 2 3
B2 4 6
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Does changing 'x' inside the loop change the original array?
Predict Output
advanced
2: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);
    }
}
A10
BCompilation error
C24
D0
Attempts:
2 left
💡 Hint
Add all numbers in the 2D array.
🧠 Conceptual
expert
2:00remaining
Behavior of enhanced for loop with Iterable and Iterator
Which statement about the enhanced for loop in Java is true?
AIt requires manual handling of the loop index variable.
BIt internally uses an Iterator to loop over elements of Iterable collections.
CIt allows modifying the collection elements directly during iteration.
DIt can only be used with arrays, not with collections.
Attempts:
2 left
💡 Hint
Think about how enhanced for loops work with collections like ArrayList.