ProgrammingVery hardProduct company OA
What is printed?
What is printed?
class A:
def go(self): return ["A"]
class B(A):
def go(self): return ["B"] + super().go()
class C(A):
def go(self): return ["C"] + super().go()
class D(B, C):
def go(self): return ["D"] + super().go()
class E(C, A):
pass
class F(D, E):
pass
print("".join(F().go()), [k.__name__ for k in F.__mro__][2:5])- A. DBA ['B', 'C', 'E']
- B. DBCA ['B', 'E', 'C']
- C. DBCA ['B', 'C', 'E']
- D. DBECA ['B', 'E', 'C']
Correct answer: B. DBCA ['B', 'E', 'C']
C3 linearisation merges [D, B, C, A, object], [E, C, A, object] and [D, E]. After D and B, C cannot be taken yet because it still appears in the tail of E's list, so E comes first: F, D, B, E, C, A, object. super() follows this MRO of the instance, not the static parent: D → B → (E has no go) → C → A, giving 'DBCA'. Slots 2..4 of the MRO are B, E, C. 'DBA' is the single-inheritance answer where B's super() is taken to be A.
More Python Programming questions
- What is printed? python def inner(): x = yield 1 y = yield x + 1 return x y def outer(): r = yield from inner() yield r yield from…
- What is printed? python def reg(name, seen={}, order=[]): if name not in seen: seen[name] = len(order) order = order + [name] return…
- What is printed? python def bucket(x, w): return x // w, x % w, int(x / w), -(-x // w) print(bucket(-7, 3), bucket(7, -3))
- What is printed? python d = {"b": 1, "a": 2, "c": 3} d["a"] = 9 d.update(b=7, d=4) d["c"] = d.pop("c") d.setdefault("a", 0) e = {d, "b"…
- What is printed? python a = [1, 2]; b = a; c = a[:] t = (a, c) a += [3] b = b + [4] c = 2 print(t[0] is a, b is a, t[1] is c, len(t[0])…
- What is printed? python a = list(range(8)) b = a[::2] a[1::2] = b[::-1] a[2:5] = [] del a[-2:] b[0] = 99 print(a, b)