nerdexam
Oracle

1Z0-809 · Question #102

Given the code fragment: ``java int i = 0; do { i++; } while (i < 0); System.out.println("Out of limits"); `` What is the result?

The correct answer is C. Out of limits. Option C is correct because a do-while loop always executes its body at least once before evaluating the condition. Here, i starts at 0, the body runs and increments i to 1, then the condition i < 0 evaluates to false (since 1 is not less than 0), so the loop exits immediately…

Question

Given the code fragment:
int i = 0;
do {
 i++;
} while (i < 0);
System.out.println("Out of limits");
What is the result?

Options

  • AHElloJavA!
  • BHello java!
  • COut of limits
  • DHElloJavA!
  • EOut of limits

How the community answered

(29 responses)
  • A
    10% (3)
  • B
    7% (2)
  • C
    79% (23)
  • D
    3% (1)

Explanation

Option C is correct because a do-while loop always executes its body at least once before evaluating the condition. Here, i starts at 0, the body runs and increments i to 1, then the condition i < 0 evaluates to false (since 1 is not less than 0), so the loop exits immediately and System.out.println("Out of limits") executes.

Options A, B, and D are wrong because the strings "HElloJavA!" and "Hello java!" appear nowhere in the code - they are pure distractors with no basis in this fragment. Option E appears identical to C in wording, but is likely intended to represent a scenario where the loop never terminates or the print is never reached - which doesn't happen here because the condition fails after just one iteration.

Memory tip: Think of do-while as "do it then ask permission" - the body always runs once no matter what. If this were a regular while loop instead, i < 0 would be checked first (false immediately), the loop would be skipped entirely, and the output would still be "Out of limits" - so the print always fires either way in this specific example.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice