Advanced OOP — Series 3

Preview — 3 of 10 questions

What is the output of the following code?

javascript
class LoggingMixin:
    def save(self):
        print("logging save")
        super().save()

class ValidationMixin:
    def save(self):
        print("validating save")
        super().save()

class Base:
    def save(self):
        print("base save")

class Document(LoggingMixin, ValidationMixin, Base):
    pass

Document().save()
Alogging save, validating save, base save
Bbase save, validating save, logging save
Clogging save, base save
Dlogging save

What is the output of the following code?

javascript
class Meters:
    def __init__(self, value):
        self.value = value
    def __eq__(self, other):
        if not isinstance(other, Meters):
            return NotImplemented
        return self.value == other.value

class Plain:
    pass

m = Meters(5)
p = Plain()
print(m == p)
print(m == 5)
ANotImplemented, NotImplemented
BFalse, False
CTypeError: '==' not supported between instances of 'Meters' and 'Plain'
DTrue, False

What happens when the following code runs?

javascript
class Buggy:
    def __setattr__(self, name, value):
        self.data = value

try:
    b = Buggy()
    b.x = 5
except RecursionError:
    print("RecursionError")
AAttributeError: 'Buggy' object has no attribute 'data'
BNo error — b.x ends up holding 5, and b.data is untouched
CRecursionError
DNo error — b.data ends up holding 5

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.