0
0
Javaprogramming~20 mins

Difference between while and do–while in Java - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
AInfinite loop
B0Done
CCompilation error
DDone
Attempts:
2 left
💡 Hint
Remember how while loop checks the condition before running the loop body.
Predict Output
intermediate
2: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");
A0Done
BDone
CInfinite loop
DCompilation error
Attempts:
2 left
💡 Hint
Remember how do-while loop runs the body before checking the condition.
🧠 Conceptual
advanced
2:00remaining
Key difference between while and do-while loops
Which statement best describes the key difference between while and do-while loops in Java?
Awhile loop checks the condition before running the loop body; do-while loop checks after running the body once.
Bwhile loop runs the body at least once; do-while loop may not run the body at all.
Cwhile loop can only be used with integers; do-while loop can be used with any data type.
Dwhile loop is faster than do-while loop in all cases.
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.
🔧 Debug
advanced
2: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);
ARuntimeException: infinite loop
BSyntaxError: missing semicolon after i++
CNo error, prints 0 1 2
DCompilation error: missing while keyword
Attempts:
2 left
💡 Hint
Check punctuation carefully in Java statements.
🚀 Application
expert
3: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);
A6 times
B0 times
C5 times
D1 time
Attempts:
2 left
💡 Hint
Analyze each loop separately and count executions carefully.