Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to override the method greet in the subclass.
Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): return [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the parent's greet method call instead of a new string.
Calling greet() without self or super().
✗ Incorrect
The Child class overrides the greet method by returning its own string.
2fill in blank
mediumComplete the code to call the parent class method greet inside the overridden method.
Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): return [1] + " and Child"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.greet() causes infinite recursion.
Calling Parent.greet() without passing self.
✗ Incorrect
Using super().greet() calls the parent method correctly inside the child method.
3fill in blank
hardFix the error in the overridden method to correctly call the parent class method.
Python
class Parent: def greet(self): return "Hello from Parent" class Child(Parent): def greet(self): return Parent.greet([1]) + " and Child"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name instead of the instance.
Using super() inside a direct class method call.
✗ Incorrect
When calling Parent.greet directly, you must pass the instance (self) explicitly.
4fill in blank
hardFill both blanks to override describe method and call the parent method inside it.
Python
class Animal: def describe(self): return "I am an animal" class Dog(Animal): def describe(self): return [1] + ", and I am a dog" obj = Dog() print(obj.describe())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.describe() causes infinite recursion.
Returning only the dog's string without calling parent.
✗ Incorrect
Using super().describe() calls the parent method; returning the string literal alone is not enough.
5fill in blank
hardFill all three blanks to override info method, call parent method, and add extra info.
Python
class Vehicle: def info(self): return "Vehicle info" class Car(Vehicle): def info(self): return [1] + ", model: " + [2] + ", year: " + [3] car = Car() print(car.info())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.info() causes infinite recursion.
Not calling the parent method at all.
✗ Incorrect
Use super().info() to get parent info, then add model and year strings.