ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def f():
try:
return [1]
finally:
pass
def g():
x = [1]
try:
return x
finally:
x.append(2)
def h():
x = 1
try:
return x
finally:
x = 2
print(f(), g(), h())- A. [1] [1] 1
- B. [1] [1, 2] 2
- C. [1] [1, 2] 1
- D. [1] [1] 2
Correct answer: C. [1] [1, 2] 1
The return value is computed before finally runs. Mutating the returned list in finally is visible (same object), but rebinding the local name x afterwards does not change what was already chosen to be returned.
More Python Programming questions
- What is printed? (Output lines are shown separated by " / ".) python class P: def init(self, x): self.x = x def eq(self, o): return self.x…
- What is printed? python class V: def init(self, n): self.n = n def add(self, o): return V(self.n + o) def radd(self, o): return V(o 10 +…
- What is printed? python class R: def init(self, n): self.n = n def len(self): return self.n def getitem(self, i): if i >= self.n: raise…
- What is printed? python class C: def init(self): self.d = {} def getattr(self, name): return f"<{name}>" def setattr(self, name, v)…
- What is printed? python class Temp: def init(self): self.c = 0 @property def c(self): return self.c @c.setter def c(self, v): if v < -273…
- What is printed? (Output lines are shown separated by " / ".) python class A: @staticmethod def s(): return "s" @classmethod def c(cls)…