ProgrammingHardPython · GATE DA · Advanced
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):
def f(self): return 'D' + super().f()
print(D().f(), [k.__name__ for k in D.__mro__])- A. DBA ['D', 'B', 'A', 'C', 'object']
- B. DBA ['D', 'B', 'C', 'A', 'object']
- C. DBCA ['D', 'B', 'A', 'C', 'object']
- D. DBCA ['D', 'B', 'C', 'A', 'object']
Correct answer: D. DBCA ['D', 'B', 'C', 'A', 'object']
C3 linearisation gives D, B, C, A, object, and super() follows the MRO of type(self), so B's super() call reaches C, not A: DBCA.
More Python Programming questions
- What is printed? python def gen(): x = yield 1 print('got', x) y = yield x 2 return y g = gen() print(next(g)) print(g.send(10)) try…
- What is printed? python funcs = [] for i in range(3): funcs.append(lambda x=i: x 10) funcs.append(lambda: i 10) print([f() for f in funcs])
- What is printed? python class Tag: items = [] n = 0 def add(self, v): self.items += [v] self.n += 1 a, b = Tag(), Tag() a.add('x')…
- What is printed? python x = 10 class K: x = 20 ys = [x + i for i in range(2)] print(K.ys)
- What is printed? python import itertools data = [1, 1, 2, 1, 1, 1, 3] print([(k, len(list(g))) for k, g in itertools.groupby(data)])…
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v s = set() try: s.add(P(1)) except…