← 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())
- A.1
- B.2
- C.None
- 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 →