0
0
Pythonprogramming~10 mins

Purpose of polymorphism 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 define a method that can be used by different classes.

Python
class Animal:
    def sound(self):
        [1]
Drag options to blanks, or click blank then click option'
Areturn 5
Bprint('Hello')
Cself.sound()
Dpass
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the method inside itself causing recursion.
Returning a value when the method should be empty.
2fill in blank
medium

Complete the code to override the method in the subclass.

Python
class Dog(Animal):
    def sound(self):
        [1]('Bark')
Drag options to blanks, or click blank then click option'
Aprint
Breturn
Cpass
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using return instead of print which won't show output.
Leaving the method empty with pass.
3fill in blank
hard

Fix the error in the code to call the correct method for each animal.

Python
def animal_sound(animal):
    [1].sound()
Drag options to blanks, or click blank then click option'
Aanimal
Banimal.sound()
Canimal()
Danimal.sound
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after the method name in the blank causing syntax errors.
Using the method name without the object.
4fill in blank
hard

Fill both blanks to create a list of animals and call their sounds using polymorphism.

Python
animals = [[1], [2]]
for a in animals:
    a.sound()
Drag options to blanks, or click blank then click option'
ADog()
BAnimal()
CCat()
DBird()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the base class Animal() which has no sound implementation.
Using class names without parentheses, which are not instances.
5fill in blank
hard

Fill all three blanks to define classes and demonstrate polymorphism with a function.

Python
class [1]:
    def sound(self):
        pass

class [2]([1]):
    def sound(self):
        print('Meow')

def make_sound(animal):
    animal.sound()

make_sound([2]())
Drag options to blanks, or click blank then click option'
AAnimal
BCat
CDog
DBird
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing class names or forgetting to inherit from the base class.
Calling the function with the wrong class instance.