Java Loops (for, while, do-while) Quiz
35 deep, practical, and often misunderstood questions about Java loops — from subtle break/continue label behavior and enhanced for-loop limitations to infinite loop detection, do-while quirks, loop unrolling myths, and the performance & safety best practices used in real production systems.
Question 1
What is the only fundamental difference between while and do-while that can never be replicated with the other?
Question 2
Why does this nested loop exit completely with a single statement?
outer: for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 3) break outer;
System.out.print(j + " ");
}
System.out.println();
}Question 3
What actually happens when continue is used with a label in nested loops?
outer: for (int i = 0; i < 3; i++) {
inner: for (int j = 0; j < 3; j++) {
if (j == 1) continue outer;
System.out.print("(" + i + "," + j + ") ");
}
}Question 4
Why does this enhanced for-loop fail at runtime?
List<String> list = Arrays.asList("a", "b", "c");
for (String s : list) {
list.remove(0);
}Question 5
What is the safest way to remove elements while iterating over a collection?
Question 6
Why is this infinite loop pattern preferred in high-performance servers?
while (true) {
Task task = queue.take(); // blocking
task.execute();
}Question 7
What is actually generated by the compiler for this enhanced for-loop?
for (String s : strings) {
System.out.println(s);
}Question 8
Why does this loop never terminate even though i is incremented?
for (int i = 0; i < 10; i += 1) {
if (i == 5) i = 0;
}Question 9
What is the only loop construct that can be safely used with try-with-resources in a way that guarantees resource closure even on continue?
Question 10
Why is this loop pattern considered extremely dangerous in production?
while (true) {
processNextMessage();
Thread.sleep(100);
}Question 11
What is the output of this tricky labeled continue?
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2) continue outer;
System.out.print(i + "" + j + " ");
}
}Question 12
Why does this enhanced for-loop work perfectly on primitive arrays?
int[] numbers = {1, 2, 3};
for (int n : numbers) {
System.out.println(n);
}Question 13
What is the most common mistake when using break in nested loops?
Question 14
Why is for(;;) considered idiomatic for infinite loops in Java?
Question 15
Why does this loop cause a performance myth to be busted?
for (int i = 0; i < list.size(); i++) {
// body
}Question 16
What is the output when break is used in this do-while?
int i = 0;
do {
if (i == 2) break;
System.out.print(i + " ");
i++;
} while (i < 5);Question 17
Why is the loop variable in enhanced for-loop effectively final?
Question 18
What is the most overlooked feature of labeled loops?
Question 19
Why is this loop considered the cleanest way to process input until EOF?
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
process(line);
}Question 20
After mastering Java loops, what is the single most important principle you now follow?
Question 21
Why does this loop print nothing despite the condition being true initially?
int x = 5;
while (x < 5) {
System.out.println(x);
x++;
}Question 22
What is the output of this continue behavior?
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
if (i == 3) break;
}Question 23
Why is the enhanced for-loop not suitable for this use case?
for (String s : list) {
if (shouldRemove(s)) list.remove(s);
}Question 24
What is the actual performance difference between these two loops in modern JVMs?
// Loop A
for (int i = 0; i < arr.length; i++) { sum += arr[i]; }
// Loop B
int len = arr.length;
for (int i = 0; i < len; i++) { sum += arr[i]; }Question 25
Why does this loop run exactly once?
int count = 0;
do {
System.out.println("Running");
count++;
} while (count < 0);Question 26
What is the most elegant way to iterate with index when you need both element and position?
Question 27
Why is this infinite loop safe and commonly used?
for (;;) {
String input = scanner.nextLine();
if ("quit".equals(input)) break;
process(input);
}Question 28
What is the output when continue skips the increment?
int i = 0;
while (i < 5) {
if (i == 3) {
continue;
}
System.out.print(i + " ");
i++;
}Question 29
Why is the enhanced for-loop the preferred choice in 99% of cases?
Question 30
What is the only valid use case for modifying the loop variable in a traditional for loop?
Question 31
Why does this loop execute exactly 10 times despite the condition?
int i = 0;
while (i++ < 10) {
System.out.print(i + " ");
}Question 32
What is the modern replacement for manual iteration in most cases?
Question 33
Why is for(;;) preferred over while(true) in performance-critical code?
Question 34
When should you actually use do-while in real code?
Question 35
You now fully understand that great Java loop code means:
