Question 7
Back to Exercise
What is the output of the following Java program?
public class Main {
public static void main(String[] args) {
int i = 10, runsWhile = 0, runsDo = 0;
while (i < 5) { runsWhile++; i++; }
do { runsDo++; i++; } while (i < 5);
System.out.println(runsWhile + " " + runsDo + " " + i);
}
}
- A 0 1 11 Correct
- B 0 0 10
- C 1 1 11
- D 0 1 10
Correct Answer: Option A
The while body never runs; the do-while body runs once before its condition is checked.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!