ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = 1
g = (x * y for y in range(3))
x = 2
print(list(g))
  1. A. [0, 1, 2]
  2. B. [0, 2, 4]
  3. C. [0, 2, 2]
  4. D. []

Correct answer: B. [0, 2, 4]

Only the outermost iterable (range(3)) is evaluated when the generator expression is created; x is looked up each time an element is produced, by which point x is 2.

More Python Programming questions

Exams that ask this topic