🎙 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 2 of 10

What does this print? (code)

What does this print?

class Logger:
    _one = None
    def __new__(cls):
        if cls._one is None:
            cls._one = super().__new__(cls)
            cls._one.lines = []
        return cls._one

Logger().lines.append('start')
Logger().lines.append('stop')
print(len(Logger().lines))
  1. A.0
  2. B.1
  3. C.2
  4. D.3
Answer & explanation

Correct answer: 2

Every Logger() returns the same object, so both appends go to one list.

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 →