ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
x = 0
def outer():
    x = 1
    def mid():
        def inner():
            nonlocal x
            x += 10
            return x
        return inner()
    r = mid()
    return r, x
print(outer(), x)
  1. A. (11, 1) 0
  2. B. (11, 11) 0
  3. C. (10, 1) 10
  4. D. (11, 11) 11

Correct answer: B. (11, 11) 0

nonlocal searches outward through enclosing function scopes and binds to outer's x (skipping mid, which has no x). So outer's x becomes 11; the global x is untouched.

More Python Programming questions

Exams that ask this topic