Bird
0
0

In this Python code, what is the error?

medium📝 Analysis Q6 of 15
LLD - Behavioral Design Patterns — Part 1
In this Python code, what is the error?
class Base:
    def template(self):
        self.step1()
        self.step2()
    def step1(self):
        print('Step 1')

class Derived(Base):
    def step2(self):
        print('Step 2')

obj = Derived()
obj.template()
AAttributeError because step2 is not defined in Base
BNo error; prints Step 1 and Step 2
CTypeError due to missing step2 argument
DSyntaxError due to missing method
Step-by-Step Solution
Solution:
  1. Step 1: Trace template method calls

    template calls self.step1() and self.step2(). step1 exists in Base, step2 only in Derived.
  2. Step 2: Check method resolution

    Since template is called on Derived, self.step2() resolves to Derived.step2(), so no error.
  3. Correction:

    Actually, step2 is called on Derived instance, so no AttributeError occurs.
  4. Final Answer:

    No error; prints Step 1 and Step 2 -> Option B
  5. Quick Check:

    Subclass method called correctly = No error; prints Step 1 and Step 2 [OK]
Quick Trick: Methods in subclass override base calls [OK]
Common Mistakes:
MISTAKES
  • Assuming base must define all methods
  • Confusing AttributeError with TypeError
  • Thinking missing method causes syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes