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)- A. o i
- B. i i
- C. o g
- 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
- What is printed? python s = " Py Thon " print(s.strip().lower().replace(" ", "").count("o"), s.split(" ").count(""))
- What is printed? python def f(x): try: r = 10 // x except ZeroDivisionError: return "Z" else: return r finally: print("F", end=" ")…
- What is printed? python class P: def repr(self): return "R" def str(self): return "S" print(P(), [P()], f"{P()!r}")
- What is printed? python class K: def eq(self, other): return True try: print({K(): 1}) except TypeError: print("TypeError")
- What is printed? python it = iter([1, 2, 3, 4, 5]) print(list(zip(it, it)))
- What is printed? python x = 1 x, y = x + 1, x 10 print(x, y)