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())- A. ['Repo', 'Log', 'Base']
- B. ['Repo', 'Log', 'Base', 'Cache', 'Base']
- C. ['Repo', 'Cache', 'Log', 'Base']
- 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.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- This code illustrates the 'fragile base class' problem. What does it print? python class Bag: def init(self): self.items = [] def…
- What does this code print? python from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): ... def…
- Under design by contract, a method sqrt(x) has the precondition x ≥ 0. A caller passes -4 and the method fails. Whose bug is it, and which…
- A service catches every exception in a broad try/except, logs nothing and returns an empty list, so a database outage looks like 'no…
- How many of these defend against SQL injection in a login query: (1) parameterised queries / prepared statements, (2) escaping quotes by…
- A document-editor framework defines an abstract Application class with a method newDocument() that calls createDocument(); each concrete…