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])
  1. A. DBA ['B', 'C', 'E']
  2. B. DBCA ['B', 'E', 'C']
  3. C. DBCA ['B', 'C', 'E']
  4. 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

Exams that ask this topic