Bird
0
0

You want to print all even numbers from 2 to 10 using a loop. Which code correctly uses a loop to do this?

hard📝 Application Q15 of 15
Python - For Loop
You want to print all even numbers from 2 to 10 using a loop. Which code correctly uses a loop to do this?
AAll of the above
Bfor i in range(2, 11): if i % 2 == 0: print(i)
Ci = 2 while i <= 10: print(i) i += 2
Dfor i in range(2, 11, 2): print(i)
Step-by-Step Solution
Solution:
  1. Step 1: Analyze for i in range(2, 11, 2): print(i)

    Uses for loop with step 2 from 2 to 10 inclusive, prints even numbers correctly.
  2. Step 2: Analyze for i in range(2, 11): if i % 2 == 0: print(i) and C

    for i in range(2, 11): if i % 2 == 0: print(i) uses for loop with condition to print evens; i = 2 while i <= 10: print(i) i += 2 uses while loop incrementing by 2. Both print even numbers correctly.
  3. Step 3: Understand All of the above

    Since A, B, and C all correctly print even numbers from 2 to 10, All of the above is correct.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    All options print evens correctly = All of the above [OK]
Quick Trick: Multiple loop styles can print even numbers correctly [OK]
Common Mistakes:
MISTAKES
  • Forgetting step in range for even numbers
  • Not including 10 in range end
  • Incorrect increment in while loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes