0
0
Pythonprogramming~10 mins

Extending parent behavior in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the parent class method inside the child class.

Python
class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        [1]
Drag options to blanks, or click blank then click option'
Aself.greet()
BParent.greet(self)
Csuper().greet()
Dgreet()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.greet() causes infinite recursion.
Calling Parent.greet() without self argument causes error.
Calling greet() alone is undefined.
2fill in blank
medium

Complete the code to extend the parent method by adding extra behavior in the child class.

Python
class Parent:
    def show(self):
        print("Parent show")

class Child(Parent):
    def show(self):
        [1]
        print("Child show")
Drag options to blanks, or click blank then click option'
Aself.show()
BParent.show(self)
Cshow()
Dsuper().show()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.show() causes infinite recursion.
Calling Parent.show(self) works but is less flexible.
Calling show() alone causes error.
3fill in blank
hard

Fix the error in the child class method to properly extend the parent method.

Python
class Parent:
    def info(self):
        print("Info from Parent")

class Child(Parent):
    def info(self):
        [1]
        print("Info from Child")

c = Child()
c.info()
Drag options to blanks, or click blank then click option'
Asuper().info()
Binfo()
CParent.info(self)
Dself.info()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.info() causes infinite recursion.
Calling Parent.info() without self argument causes error.
Calling info() alone is undefined.
4fill in blank
hard

Fill both blanks to extend the parent method and add a new message in the child class.

Python
class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def speak(self):
        [1]
        print([2])
Drag options to blanks, or click blank then click option'
Asuper().speak()
B"Dog barks"
C"Dog says woof"
DAnimal.speak(self)
Attempts:
3 left
💡 Hint
Common Mistakes
Using Animal.speak(self) works but is less flexible than super().speak().
Printing the wrong message confuses output.
Not calling the parent method misses inherited behavior.
5fill in blank
hard

Fill all three blanks to extend the parent method, add a new message, and call another parent method.

Python
class Vehicle:
    def start(self):
        print("Vehicle started")
    def stop(self):
        print("Vehicle stopped")

class Car(Vehicle):
    def start(self):
        [1]
        print([2])
        [3]
Drag options to blanks, or click blank then click option'
Asuper().start()
B"Car engine running"
Csuper().stop()
DVehicle.start(self)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Vehicle.start(self) works but is less flexible than super().start().
Not calling super().stop() misses stopping behavior.
Printing wrong message confuses output.