What is the enhanced for loop (for-each) in Java?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Java basics — a prerequisite for any developer role.
Answer
The enhanced for loop (also called for-each) provides a cleaner syntax for iterating over arrays and any class that implements the Iterable interface: for (String name : names) { System.out.println(name); }. It eliminates the need for index management and is less error-prone than traditional index-based loops. Drawbacks: you cannot modify the underlying collection while iterating (throws ConcurrentModificationException), you cannot access the current index, and you cannot iterate in reverse. For these cases, use a traditional for loop, an Iterator, or Java 8 streams with forEach.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong Java candidates.