Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the abstract method in the base class.
LLD
class AbstractClass: def template_method(self): self.base_operation1() self.[1]() self.base_operation2() def base_operation1(self): print("Base operation 1") def base_operation2(self): print("Base operation 2") def [1](self): pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the template_method name for the abstract method.
Overriding base_operation1 instead of the abstract method.
✗ Incorrect
The Template Method pattern requires defining an abstract method (primitive_operation) that subclasses override.
2fill in blank
mediumComplete the code to override the primitive operation in the subclass.
LLD
class ConcreteClass(AbstractClass): def [1](self): print("Concrete implementation of primitive operation")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Overriding template_method instead of primitive_operation.
Not overriding any method.
✗ Incorrect
Subclasses override the primitive_operation method to provide specific behavior.
3fill in blank
hardFix the error in the template method to call the correct primitive operation.
LLD
def template_method(self): self.base_operation1() self.[1]() self.base_operation2()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling template_method recursively causing infinite loop.
Calling base_operation1 twice.
✗ Incorrect
The template method must call the primitive_operation to allow subclass customization.
4fill in blank
hardFill both blanks to add a hook method and call it in the template method.
LLD
class AbstractClass: def template_method(self): self.base_operation1() self.primitive_operation() self.[1]() self.base_operation2() def [2](self): pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both blanks incorrectly.
Not defining the hook method at all.
✗ Incorrect
The hook method is optional and called in the template method; it is defined as an empty method in the base class.
5fill in blank
hardFill all three blanks to implement a concrete hook method and call the template method.
LLD
class ConcreteClass(AbstractClass): def primitive_operation(self): print("Concrete primitive operation") def [1](self): print("Concrete hook operation") obj = ConcreteClass() obj.[2]() # Output should show base_operation1, primitive_operation, hook, base_operation2 # Call the template method named [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling primitive_operation directly instead of template_method.
Not overriding the hook method.
✗ Incorrect
The concrete hook method overrides the base hook; the template_method is called on the object to run the algorithm.
