🎙 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 3 of 10

What does this print? (code)

What does this print?

def make():
    n = 0
    def step():
        nonlocal n
        n += 5
        return n
    return step
s = make()
s()
print(s())
  1. A.5
  2. B.0
  3. C.10
  4. D.An error
Answer & explanation

Correct answer: 10

The closure keeps n alive between calls: 5 after the first call, 10 after the second.

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 →