CS FundamentalsVery hardProduct-company placement · Python interview

Mixins rely on Python's method resolution order. What does this print?

Mixins rely on Python's method resolution order. What does this print?
class Base:
    def save(self): return ["Base"]
class Log(Base):
    def save(self): return ["Log"] + super().save()
class Cache(Base):
    def save(self): return ["Cache"] + super().save()
class Repo(Log, Cache):
    def save(self): return ["Repo"] + super().save()
print(Repo().save())
  1. A. ['Repo', 'Log', 'Base']
  2. B. ['Repo', 'Log', 'Base', 'Cache', 'Base']
  3. C. ['Repo', 'Cache', 'Log', 'Base']
  4. D. ['Repo', 'Log', 'Cache', 'Base']

Correct answer: D. ['Repo', 'Log', 'Cache', 'Base']

The C3 linearisation of Repo is Repo, Log, Cache, Base, object. super() follows this MRO, not the static parent, so Log's super() calls Cache, and Base runs exactly once.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic