← Quiz: Design Patterns: all 10 questions
Quiz: Design Patterns · Question 5 of 10
What does this print? (code)
What does this print?
class Base:
def cost(self):
return 100
class Add:
def __init__(self, inner, extra):
self.inner, self.extra = inner, extra
def cost(self):
return self.inner.cost() + self.extra
print(Add(Add(Base(), 20), 30).cost())
- A.100
- B.130
- C.120
- D.150
Answer & explanation
Correct answer: 150
Each wrapper adds its extra to the cost of what it wraps: 100 + 20 + 30.
Can you get the rest right without peeking?
Take the timed Quiz: Design Patterns quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →