ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class A:
def who(self): return "A"
class B(A):
def who(self): return "B"
class C(A):
def who(self): return "C"
class D(B, C):
pass
print(D().who())
print([c.__name__ for c in D.__mro__])- A. A ['D', 'A', 'B', 'C', 'object']
- B. C ['D', 'B', 'C', 'A', 'object']
- C. B ['D', 'B', 'C', 'A', 'object']
- D. B ['D', 'C', 'B', 'A', 'object']
Correct answer: C. B ['D', 'B', 'C', 'A', 'object']
D inherits who from the nearest class in C3 linearization order, which is B. The MRO visits D, then B, then C, then A, then object, so D().who() is 'B'.
More Python Programming questions
- What is printed? python class A: def f(self): return 1 class B(A): def f(self): return 2 + super().f() class C(B): def f(self): return 3 +…
- What is printed? python class F: @staticmethod def add(a, b): return a + b @classmethod def create(cls): return cls() print(F.add(2, 3)) f…
- What is printed? python class H: slots = ["a", "b"] h = H() h.a = 1 try: h.c = 2 except AttributeError: print("AttributeError") print(h.a)
- What is printed? python class K: x = 10 def method(self): x = 20 return x print(K().method(), K.x)
- What is printed? python class Counter: def init(self, n): self.n = n def iter(self): self.i = 0 return self def next(self): if self.i >=…
- What is printed? python class Box: def init(self, items): self.items = items def len(self): return len(self.items) def contains(self, x)…