Exercise: Polymorphism

Questions for: Polymorphism

Consider the following code:
class MusicInstrument:
    def play(self):
        return "Playing music instrument"

class Guitar(MusicInstrument):
    def play(self):
        return "Strumming the guitar"

class Piano(MusicInstrument):
    def play(self):
        return "Playing the piano"
What concept is demonstrated in this code?
A:
Method overloading
B:
Method overriding
C:
Operator overloading
D:
Polymorphism
Answer: B
This code demonstrates method overriding, where the subclasses provide specific implementations for a method defined in the superclass.
In Python, what is the purpose of the __len__() method in the context of polymorphism?
A:
To define class attributes
B:
To customize the behavior when an instance is checked for equality using the == operator
C:
To customize the behavior when the len() function is called on an instance
D:
To create a new instance of the class
Answer: C
The __len__() method is used to customize the behavior when the len() function is called on an instance of the class, allowing for polymorphic behavior.
Consider the following Python code:
class Bird:
    def fly(self):
        return "Bird flying"

class Penguin(Bird):
    def fly(self):
        return "Penguin can't fly"
    
def display_flying_ability(bird):
    return bird.fly()

bird = Bird()
penguin = Penguin()

print(display_flying_ability(bird))
print(display_flying_ability(penguin))
A:
Bird flying\nPenguin can't fly
B:
Penguin can't fly\nBird flying
C:
Bird flying\nBird flying
D:
Penguin can't fly\nPenguin can't fly
Answer: B
The display_flying_ability() function demonstrates polymorphism, accepting both Bird and Penguin instances and producing different outputs based on their specific implementations of the fly() method.
What is the output of the following Python code?
class Car:
    def start(self):
        return "Car starting"

class ElectricCar(Car):
    def start(self):
        return "Electric car starting"

def drive(car):
    return car.start()

car = Car()
electric_car = ElectricCar()

print(drive(car))
print(drive(electric_car))
A:
Car starting\nCar starting
B:
Electric car starting\nElectric car starting
C:
Car starting\nElectric car starting
D:
Electric car starting\nCar starting
Answer: C
The drive() function demonstrates polymorphism, accepting both Car and ElectricCar instances and producing different outputs based on their specific implementations of the start() method.
What does the following Python code demonstrate?
class Animal:
    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return "Woof!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

def pet_sound(animal):
    return animal.speak()

dog = Dog()
cat = Cat()

print(pet_sound(dog))
print(pet_sound(cat))
A:
Method overloading
B:
Method overriding
C:
Operator overloading
D:
Polymorphism
Answer: D
The pet_sound() function demonstrates polymorphism as it can accept different types of animals (instances of Dog and Cat) and produce different sounds based on their specific implementations.
Ad Slot (Above Pagination)
Quiz