What Does a for-Loop Stepping by Two Print?

Controlling program flow
Answer Correct answer: D — With the counter starting at 1 and stepping by two while it stays below 10, the loop prints only the odd values 1 3 5 7 9.

Given the code fragment: What is the result? - image

  1. 2 4 6 8
  2. 2 4 6 8 10
  3. 1 3 5 7 9 11
  4. 1 3 5 7 9 Correct Answer

Community Votes

D
100%

100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The question tests tracing a pre-test counting loop through its boundary check; the trap is appending the next out-of-range value (11) to the printed output.

A Java for-loop that starts its counter at 1 and advances by two while the counter stays below 10 prints exactly the odd values 1 3 5 7 9. This page confirms option D as the result and explains why the off-by-one alternatives fail.

Picking C, the full odd sequence through 11, because learners list one more value than the loop condition (i < 10) actually allows the body to print.

Community Discussion (4 comments)

ken95jr 👍 1 Selected: D
D is correct tested
d7bb0b2 👍 1 Selected: D
D is correct 1- print 1 2- print 3 3- print 5 4- print 7 5 - print 9 then ++i => 10 finish
d7bb0b2 👍 1
D is correct , cause print impar begin in 1 and finish when i = 10
Felipe47 👍 1
Tested. D

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The fragment is a straightforward counting loop whose control variable starts at 1 and advances by two on each pass while the condition i < 10 still holds. Every iteration prints the current value, so the output is 1 3 5 7 9 and nothing else — even values are never produced and the terminating bound is respected. Because the condition is evaluated before the body (a pre-test loop), the next odd candidate is rejected the moment it fails the test. Community learners who actually compiled and ran the snippet confirm this result; d7bb0b2 traced the output step by step as "print 1 2- print 3 3- print 5 4- print 7 5 - print 9", and ken95jr states "D is correct tested". Option D is therefore the only sequence that matches both the starting value and the loop bound.

Why the Other Options Are Wrong

Options A (2 4 6 8) and B (2 4 6 8 10) are the even-value sequences; they could only appear if the counter were initialized to 2 or if the parity of the printed value were inverted, neither of which the fragment does. Option C (1 3 5 7 9 11) contains the correct odd values but adds one extra element, ignoring that the loop condition stops the iteration before the value 11 is ever printed. Option D is the only output that starts at 1, steps by two, and halts before reaching the bound — exactly what the control flow of the fragment produces. Recognizing that C is an off-by-one distractor is the whole point of the question.

Community Comment Notes

The thread is unanimous on D, but the real value lies in the execution traces rather than the vote count. d7bb0b2 walked through each printed value and then noted the counter advancing with "then ++i => 10 finish", i.e. the loop exits once the condition fails. ken95jr and Felipe47 both report running the code, with Felipe47 summarizing it as simply "Tested. D". That kind of on-machine verification is precisely the habit that makes loop-output questions on 1Z0-819 quick and safe to answer.

How to Verify It Yourself

Mentally (or actually) write three columns — counter value, condition result, printed value — for each pass until the condition first returns false. The final row of that table is the one that eliminates the extra-element distractor, and it is a technique that transfers to every do-while, while and nested-loop trace on the exam.

Official Reference

Exam Strategy

Trace the counter, the condition check and the printed value for each pass in a small table before reading the options. The option that contains exactly one extra element (here 11) is the classic off-by-one distractor on 1Z0-819 control-flow questions.

Frequently Asked Questions

Why doesn't the loop also print 11 as option C suggests?

The loop condition is tested before each pass; once the counter is no longer less than 10 the body is skipped, so 11 is never printed.

Why is option C (1 3 5 7 9 11) the most tempting distractor?

It lists the correct odd values but ignores the terminating bound, adding one iteration the loop never executes — a classic off-by-one trap.

More 1Z0-819 FAQ →

Related Analysis

← Back to 1Z0-819 Study Guide