Loading
Java Enhanced For Loop-interview

Q1. What is an enhanced for loop in Java and why was it introduced?

The enhanced for loop, also called for-each loop, was introduced in Java 5 to simplify iteration over arrays and collections without needing indexes.





Q2. Can we use enhanced for loop for both arrays and collections?

Yes, it works for both arrays and collections like ArrayList, HashSet, etc.





Q3. What is the main limitation of the enhanced for loop?

It doesn’t provide index access and doesn’t allow direct modification of elements.





Q4. How is enhanced for loop different from a normal for loop?

Normal for loop gives index control, while enhanced for loop is simpler and more readable but index-independent.




Q5. Can we use break and continue inside an enhanced for loop?

Yes, both break and continue can be used to control loop execution.