ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class A:
def f(self): return "A"
class B(A):
def f(self): return "B" + super().f()
class C(A):
def f(self): return "C" + super().f()
class D(B, C):
pass
class E(C, B):
def f(self): return "E" + super().f()
print(D().f(), E().f())- A. BCA ECBA
- B. BA ECA
- C. BCA ECA
- D. BA ECBA
Correct answer: A. BCA ECBA
super() follows the MRO of the actual object: for D it is D, B, C, A, so B.f calls C.f, then A. For E it is E, C, B, A, so E.f -> C.f -> B.f -> A.f.
More Python Programming questions
- What is printed? python class A: x = 1 class B(A): pass class C(A): x = 3 class D(B, C): pass B.x = 2 del B.x print(D.x, D.mro.index(C))
- What is printed? python class Shape: sides = 0 def describe(self): return f"{type(self).name}:{self.sides}" class Tri(Shape): sides = 3…
- What is printed? python class Money: def init(self, v): self.v = v def lt(self, o): return self.v < o.v def repr(self): return…
- What is printed? python class A: def eq(self, o): print("A.eq", end=" ") return NotImplemented class B(A): def eq(self, o): print("B.eq"…
- What is printed? python class Lazy: def init(self): self.calls = 0 @property def value(self): self.calls += 1 return 42 from functools…
- What is printed? python class Pos: def setname(self, owner, name): self.name = "" + name def get(self, obj, typ=None): return getattr(obj…