ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
x = 10
def outer():
    x = 20
    def inner():
        nonlocal x
        x += 1
        return x
    return inner() + x
print(outer(), x)
  1. A. 41 10
  2. B. 42 10
  3. C. 42 21
  4. D. 31 10

Correct answer: B. 42 10

nonlocal makes inner modify outer's x (20 → 21). inner() returns 21 and is evaluated before x is read in the sum, so 21 + 21 = 42. The global x is untouched (10).

More Python Programming questions

Exams that ask this topic