ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita
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())- A. ['D', 'B', 'A']
- B. ['D', 'C', 'B', 'A']
- C. ['D', 'B', 'C', 'A']
- D. ['D', 'B', 'C', 'A', 'A']
Correct answer: C. ['D', 'B', 'C', 'A']
Method resolution order for D is D, B, C, A, and each super() call moves to the next class in that order. The result therefore visits B before C.
More Python Programming questions
- What is printed? python from itertools import product print(len(list(product('01', repeat=3))), list(product('ab', 'x'))[-1], sum(1 for in…
- What is printed? python import heapq, itertools c = itertools.count() h = [] for p, name in [(2, 'x'), (1, 'y'), (2, 'z'), (1, 'w')]…
- What is printed? python d = dict.fromkeys(['a', 'b'], []) d['a'].append(1) print(d, d['b'] is d['a'])
- What is printed? python def pc(x): c = 0 while x: x &= x - 1 c += 1 return c print(pc(0b1011011), pc(255), pc(0))
- What is printed? python def rev8(x): r = 0 for in range(8): r = (r << 1) | (x & 1) x >>= 1 return r print(rev8(1), rev8(0b00010110)…
- What is printed? python def maj(a): cand, cnt = None, 0 for x in a: if cnt == 0: cand = x cnt += 1 if x == cand else -1 return cand…