Conditions & Loops · Question 20 · Hard
continue comes after the increment inside a while loop. What list is built?
i = 0
out = []
while i < 6:
i += 1
if i % 2:
continue
out.append(i)
print(out)
- [2, 4]
- [1, 3, 5]
- [0, 2, 4]
- [2, 4, 6]
Answer and explanation
Answer: D. [2, 4, 6]
Explanation: i is increased first, so the body sees 1 to 6. For odd values i % 2 is 1, which is true, and continue skips the append. The even values 2, 4 and 6 are added; 6 is included because the test i < 6 was made when i was 5.
Previous: Both conditions of this if-elif contain a division by x, and x is 0. What is…
Practise conditions & loops
Try a fresh set of conditions & loops questions with an explanation after every answer, free and without signing up: Conditions & Loops practice. Or sit a full placement mock test.
More conditions & loops questions
- A while loop with an else clause tests whether 91 has a small divisor. What…
- The loop variable is changed inside the body of a for loop. What does this…
- Both conditions of this if-elif contain a division by x, and x is 0. What is…
More python practice
Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge