Bird
0
0

Which of the following while loops correctly prints all even numbers from 2 to 10 inclusive?

hard📝 Application Q8 of 15
Python - While Loop

Which of the following while loops correctly prints all even numbers from 2 to 10 inclusive?

A<pre>counter = 2 while counter < 10: print(counter) counter += 2</pre>
B<pre>counter = 1 while counter <= 10: if counter % 2 == 0: print(counter) counter += 1</pre>
C<pre>counter = 0 while counter < 10: counter += 2 print(counter)</pre>
D<pre>counter = 2 while counter <= 10: print(counter) counter += 2</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct start and end values

    The loop should start at 2 and include 10.
  2. Step 2: Check increment steps

    Incrementing by 2 ensures only even numbers are printed.
  3. Step 3: Verify loop condition

    The condition should allow printing 10 (<= 10).
  4. Final Answer:

    counter = 2
    while counter <= 10:
        print(counter)
        counter += 2
    correctly prints even numbers 2,4,6,8,10.
  5. Quick Check:

    Start at 2, increment by 2, stop at 10 [OK]
Quick Trick: Increment counter by 2 from 2 to 10 [OK]
Common Mistakes:
MISTAKES
  • Starting counter at 1 and missing even numbers
  • Using < instead of <= and missing 10
  • Printing before increment causing off-by-one errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes