Conditions & Loops · Question 16 · Hard
break appears inside the inner of two nested loops. Which pairs are collected?
out = []
for i in range(3):
for j in range(3):
if j == i:
break
out.append((i, j))
print(out)
- []
- [(1, 0), (2, 0), (2, 1)]
- [(1, 0)]
- [(0, 1), (0, 2), (1, 2)]
Answer and explanation
Answer: B. [(1, 0), (2, 0), (2, 1)]
Explanation: break ends only the inner loop; the outer loop carries on with the next i. For i = 0 the inner loop breaks at once, for i = 1 it adds (1, 0) and breaks at j = 1, and for i = 2 it adds (2, 0) and (2, 1) before breaking.
Previous: A while loop halves even numbers and applies 3n + 1 to odd numbers until n… · Next: A while loop with an else clause tests whether 91 has a small divisor. What…
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 helper function prints its argument each time it is called inside and / or…
- When does the else block attached to a for loop run?
- A while loop halves even numbers and applies 3n + 1 to odd numbers until n…
- 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…
- continue comes after the increment inside a while loop. What list is built?
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