🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Scope and Closures: all 10 questions

Quiz: Scope and Closures · Question 2 of 10

What does this print? (code)

What does this print?

x = 1
def f():
    x = 2
    def g():
        return x
    return g()
print(f())
  1. A.1
  2. B.2
  3. C.None
  4. D.An error
Answer & explanation

Correct answer: 2

g has no local x, so Python finds x in the enclosing function f.

Can you get the rest right without peeking?

Take the timed Quiz: Scope and Closures quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →