Bird
0
0

Given the following handler chain code snippet, what will be the output when handler1.handle(15) is called?

medium📝 Analysis Q4 of 15
LLD - Behavioral Design Patterns — Part 1
Given the following handler chain code snippet, what will be the output when handler1.handle(15) is called?
class Handler:
    def __init__(self, next_handler=None):
        self.next_handler = next_handler
    def handle(self, request):
        if request < 10:
            return "Handled by handler1"
        elif self.next_handler:
            return self.next_handler.handle(request)
        else:
            return "Not handled"

handler3 = Handler()
handler2 = Handler(handler3)
handler1 = Handler(handler2)
A"Handled by handler1"
B"Handled by handler2"
C"Handled by handler3"
D"Not handled"
Step-by-Step Solution
Solution:
  1. Step 1: Trace the request through the chain

    handler1 checks if 15 < 10 (false), passes to handler2; handler2 checks if 15 < 10 (false), passes to handler3; handler3 checks if 15 < 10 (false), no next handler, returns "Not handled".
  2. Step 2: Understand default handler behavior

    Since none of the handlers handle the request, the final output is "Not handled".
  3. Final Answer:

    "Not handled" -> Option D
  4. Quick Check:

    Request 15 not handled by any handler = "Not handled" [OK]
Quick Trick: Check each handler's condition carefully [OK]
Common Mistakes:
MISTAKES
  • Assuming all handlers handle requests
  • Ignoring default behavior of base handler
  • Confusing handler references

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes