← 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)
- A.[10]
- B.[10, 6]
- C.[6, 10]
- 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 →