ProgrammingModerateProduct-company MCQ pattern

What is printed?

What is printed?
print([(i, j) for i in range(3) for j in range(i)])
  1. A. [(0, 1), (0, 2), (1, 2)]
  2. B. [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (2, 2)]
  3. C. []
  4. D. [(1, 0), (2, 0), (2, 1)]

Correct answer: D. [(1, 0), (2, 0), (2, 1)]

The for clauses nest left to right like ordinary loops, so the inner j loop runs over range(i). For i = 0 it runs no times, for i = 1 it gives j = 0, and for i = 2 it gives j = 0 and 1.

More Python Programming questions

Exams that ask this topic