Bird
0
0

Identify the error in this Observer pattern code snippet:

medium📝 Analysis Q6 of 15
LLD - Behavioral Design Patterns — Part 1

Identify the error in this Observer pattern code snippet:

class Subject:
    def __init__(self):
        self.observers = []
    def addObserver(self, observer):
        self.observers.append(observer)
    def notify(self, message):
        for obs in self.observers:
            obs.update()

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

s = Subject()
o = Observer()
s.addObserver(o)
s.notify('Test')
Anotify calls update without required message argument
BObserver class missing update method
CaddObserver method does not add observer
DSubject does not store observers
Step-by-Step Solution
Solution:
  1. Step 1: Check notify method call

    Notify calls obs.update() without passing message argument.
  2. Step 2: Check Observer update signature

    Observer's update requires a message parameter, causing a TypeError.
  3. Final Answer:

    notify calls update without required message argument -> Option A
  4. Quick Check:

    Method call arguments must match definition [OK]
Quick Trick: Pass required arguments when calling update method [OK]
Common Mistakes:
MISTAKES
  • Ignoring method signature mismatch
  • Assuming update can be called without arguments
  • Overlooking error in notify loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes