🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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())
  1. A.100
  2. B.130
  3. C.120
  4. 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 →