0
0
Javaprogramming~20 mins

Do–while loop in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Do–while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple do–while loop
What is the output of the following Java code?
Java
int count = 1;
do {
    System.out.print(count + " ");
    count++;
} while (count <= 3);
A1 2
B1 2 3
C2 3
DNo output
Attempts:
2 left
💡 Hint
Remember, do–while executes the loop body at least once before checking the condition.
Predict Output
intermediate
2:00remaining
Value of variable after do–while loop
What is the value of variable x after this code runs?
Java
int x = 5;
do {
    x -= 2;
} while (x > 0);
A-1
B1
C0
D3
Attempts:
2 left
💡 Hint
Check how x changes each loop and when the loop stops.
🔧 Debug
advanced
2:00remaining
Identify the error in this do–while loop
What error does this code produce when compiled?
Java
int i = 0;
do {
    System.out.println(i);
    i++
} while (i < 3);
ASyntaxError: missing parentheses in while
BRuntimeException: infinite loop
CNo error, prints 0 1 2
DSyntaxError: missing semicolon
Attempts:
2 left
💡 Hint
Look carefully at the line before the while condition.
Predict Output
advanced
2:00remaining
Output with break inside do–while loop
What is the output of this code?
Java
int n = 1;
do {
    if (n == 3) break;
    System.out.print(n + " ");
    n++;
} while (n <= 5);
A1 2 3 4 5
B1 2 3
C1 2
DNo output
Attempts:
2 left
💡 Hint
The break stops the loop when n equals 3.
🧠 Conceptual
expert
2:00remaining
Minimum iterations of do–while vs while loop
Which statement is true about do–while loops compared to while loops?
AA do–while loop always executes its body at least once, even if the condition is false initially.
BA do–while loop may never execute its body if the condition is false initially.
CA while loop always executes its body at least once, even if the condition is false initially.
DBoth do–while and while loops execute their body the same number of times regardless of the condition.
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop type.