ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
x = "global"
def f():
    return [x for _ in range(1)][0]
def g():
    x = "local"
    return (lambda: x)()
def h():
    return eval("x")
print(f(), g(), h())
  1. A. global global global
  2. B. global local global
  3. C. local local global
  4. D. global local local

Correct answer: B. global local global

f's comprehension reads the global x. In g the lambda closes over g's local x. eval inside h uses h's (empty) locals and the module globals, so it finds 'global'.

More Python Programming questions

Exams that ask this topic