ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = 1
def f():
    print(x, end=" ")
def g():
    x = 2
    f()
g()
h = lambda: x
x = 3
print(h())
  1. A. 2 3
  2. B. 2 2
  3. C. 1 3
  4. D. 1 1

Correct answer: C. 1 3

Python is lexically scoped: f looks up x in the global scope where it was defined, not in its caller g. The lambda reads the global x when called, by then 3.

More Python Programming questions

Exams that ask this topic