1Z0-811 · Question #45
Given the code int num = 100; int count = 0; do { num--; count++; } while (count > 1); System.out.println ("num = " + num); What is the result?
The correct answer is C. num = 99. Option C is correct because a do-while loop always executes its body at least once before checking the condition. On that single execution, num decrements from 100 to 99 and count increments from 0 to 1 - then the condition count > 1 evaluates to 1 > 1, which is false, so the…
Question
Options
- AThe program executes indefinitely.
- Bnum = 100
- Cnum = 99
- Dnum = 0
How the community answered
(19 responses)- A5% (1)
- B5% (1)
- C89% (17)
Explanation
Option C is correct because a do-while loop always executes its body at least once before checking the condition. On that single execution, num decrements from 100 to 99 and count increments from 0 to 1 - then the condition count > 1 evaluates to 1 > 1, which is false, so the loop exits immediately.
A is wrong because the loop does terminate: count becomes 1 after the first pass, and 1 > 1 is false, so execution stops cleanly. B is wrong because num-- runs before any condition is checked - there's no way for num to stay at 100. D is wrong because the loop body runs only once, not 100 times; that would require the condition to keep count below some threshold and eventually reach 0.
Memory tip: Think of do-while as "act first, ask permission later" - the body always fires once regardless of the condition, which is the key trap this question is testing.
Topics
Community Discussion
No community discussion yet for this question.