ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
x = 10
def f():
    return x
x = 20
print(f(), (lambda y=x: y)(), [lambda: i for i in range(2)][1]())
  1. A. 10 20 1
  2. B. 20 10 1
  3. C. 20 20 1
  4. D. 20 20 0

Correct answer: C. 20 20 1

f looks up x when it is called, so it sees 20. The default of y is taken when the lambda is created, which is after x becomes 20. The comprehension lambda returns the loop variable, which is 1 at the end of the loop.

More Python Programming questions

Exams that ask this topic