ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
x = [1, 2]
g = (n * 10 for n in x)
x.append(3)
x = [7]
print(list(g))
  1. A. [10, 20, 30]
  2. B. [10, 20]
  3. C. [70]
  4. D. []

Correct answer: A. [10, 20, 30]

A generator expression evaluates its first iterable (x) immediately and stores the list object, but runs the loop lazily. append(3) changes that same list, so 3 is included. Rebinding x to [7] afterwards does not affect the stored object.

More Python Programming questions

Exams that ask this topic