ProgrammingVery hardGATE CS · Product-company placement

What is printed?

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

Correct answer: A. 1 3 3

f always reads the module-level x (lexical scoping), not the caller's local. g's local x = 2 is invisible to f; h rebinds the global to 3, which f then sees.

More Python Programming questions

Exams that ask this topic