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

What does this print? (code)

What does this print?

subs = []
def subscribe(fn):
    subs.append(fn)
def publish(x):
    for fn in subs:
        fn(x)

total = []
subscribe(lambda v: total.append(v * 2))
subscribe(lambda v: total.append(v + 1))
publish(5)
print(total)
  1. A.[10]
  2. B.[10, 6]
  3. C.[6, 10]
  4. D.[5, 5]
Answer & explanation

Correct answer: [10, 6]

Both subscribers run in the order they subscribed: 5 × 2 = 10, then 5 + 1 = 6.

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 →