ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
x = 5
def f():
    return x
x = 6
print(f(), (lambda: x)())
  1. A. 6 6
  2. B. 5 6
  3. C. 6 5
  4. D. 5 5

Correct answer: A. 6 6

Closures look up the variable when they are called, not when they are defined, so both functions see the final value 6.

More Python Programming questions

Exams that ask this topic