ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
x = "g"
def outer():
    x = "o"
    def inner():
        global x
        x = "i"
    inner()
    return x

print(outer(), x)
  1. A. o i
  2. B. i i
  3. C. o g
  4. D. i g

Correct answer: A. o i

global x inside inner rebinds the MODULE-level x to "i". It skips outer's local x, which stays "o" and is what outer returns. Only nonlocal would have changed outer's x.

More Python Programming questions

Exams that ask this topic