Bird
0
0

Consider this simplified code snippet for a notification system:

medium📝 Analysis Q13 of 15
LLD - Design — Online Shopping Cart
Consider this simplified code snippet for a notification system:
class Subject:
    def __init__(self):
        self.observers = []
    def subscribe(self, observer):
        self.observers.append(observer)
    def notify(self, message):
        for obs in self.observers:
            obs.update(message)

class Observer:
    def update(self, message):
        print(f"Received: {message}")

subject = Subject()
obs1 = Observer()
subject.subscribe(obs1)
subject.notify("State changed")
What will be the output when subject.notify("State changed") is called?
AError: update method missing
BNo output
CReceived: State changed
DReceived: None
Step-by-Step Solution
Solution:
  1. Step 1: Trace subscription and notification

    Observer obs1 is subscribed to subject, so it is in the observers list.
  2. Step 2: Check notify method behavior

    notify calls update on each observer with the message "State changed".
  3. Final Answer:

    Received: State changed -> Option C
  4. Quick Check:

    Observer prints message on notify [OK]
Quick Trick: Subscribed observers receive and print messages [OK]
Common Mistakes:
  • Assuming notify does nothing without explicit call
  • Thinking update method is missing
  • Expecting no output if observers list is empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes