Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of while loop with false condition
What is the output of this Java code snippet?
Java
int count = 0; while (count > 0) { System.out.print(count); count--; } System.out.print("Done");
Attempts:
2 left
💡 Hint
Remember how while loop checks the condition before running the loop body.
✗ Incorrect
The while loop condition is false initially (count=0 > 0 is false), so the loop body never runs. Only "Done" is printed.
❓ Predict Output
intermediate2:00remaining
Output of do-while loop with false condition
What is the output of this Java code snippet?
Java
int count = 0; do { System.out.print(count); count--; } while (count > 0); System.out.print("Done");
Attempts:
2 left
💡 Hint
Remember how do-while loop runs the body before checking the condition.
✗ Incorrect
The do-while loop runs the body once before checking the condition. It prints 0, then condition is false, then prints "Done".
🧠 Conceptual
advanced2:00remaining
Key difference between while and do-while loops
Which statement best describes the key difference between while and do-while loops in Java?
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.
✗ Incorrect
The while loop checks the condition before executing the loop body, so it may not run at all. The do-while loop runs the body once before checking the condition.
🔧 Debug
advanced2:00remaining
Identify the error in this do-while loop
What error will this Java code produce?
Java
int i = 0; do { System.out.println(i); i++; } while (i < 3);
Attempts:
2 left
💡 Hint
Check punctuation carefully in Java statements.
✗ Incorrect
The statement 'i++' is missing a semicolon at the end, causing a syntax error.
🚀 Application
expert3:00remaining
Number of times loop body executes
Consider this code snippet. How many times will the loop body execute?
Java
int x = 5; while (x < 5) { System.out.println(x); x--; } do { System.out.println(x); x--; } while (x > 0);
Attempts:
2 left
💡 Hint
Analyze each loop separately and count executions carefully.
✗ Incorrect
The while loop condition is false initially (5 < 5 is false), so it runs 0 times. The do-while loop runs 5 times, printing 5,4,3,2,1. After printing 1, x becomes 0, and 0 > 0 is false.