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. []
  2. [(1, 0), (2, 0), (2, 1)]
  3. [(1, 0)]
  4. [(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

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