08 - Polymorphism

 1. Polymorphism is a key pillar of OOP and it allows objects in a class or subclass to be processed differently, making use of the __________




  inheritance structure

 2. An example of polymorphism: Where 'shape' is the original class, a method to calculate area will exist in subclass objects (e.g. rectangle and circle) but the programmer will _____________

  define the area method so that it remains the same

  define a static object that never changes

  define the area method to suit the particular subclass (e.g. length x breadth for rectangle and PI x radius squared for a circle)

  None of the above

 3. Another definition of polymorphism is: It is the characteristic of an entity to be able to present in _____________

  polymorphed format (which means in a particular shape or structure) that remains the same

  a single form to avoid corruption

  polymath format (e.g numeric format)

  more than one form (have multiple behaviours)

 4. In the real world, you may inherit a trait from your parent (like dancing) but you implement it slightly differently. This is somewhat similar to _____________


  polymorphism when applied only to numbers

  the polymorphic property of overriding

  None of the above

 5. What is the use of duck typing?

  Makes the program code smaller

  More restriction on the type values that can be passed to a given method

  Less restriction on the type values that can be passed to a given method

  No restriction on the type values that can be passed to a given method

 6. In Python, any set of classes with a common set of methods can be treated similarly. This is called ___________.____________ imposes less restrictions.

  None of the above

  polymorphic overloading

  duck typing


 7. In the following code the output is 1,1,1 because the super().___init___ in the subclasses have been correctly invoked.
class A:
    def __str__(self):
        return '1'
class B(A):
    def __init__(self):
class C(B):
    def __init__(self):
def main():
    obj1 = B()
    obj2 = A()
    obj3 = C()
    print(obj1, obj2,obj3)



 8. How is the following example demonstrating polymorphism?
class Animal:
    def __init__(self, name):    # Constructor of the class = name
    def talk(self):              # Abstract method, defined by convention only
        raise NotImplementedError("Subclass must implement abstract method")

class Cat(Animal):
    def talk(self):
        return 'Meow!'

class Dog(Animal):
    def talk(self):
        return 'Woof! Woof!'

animals = [Cat('Missy'),
           Cat('Mr. Mistoffelees'),

for animal in animals:
    print ( + ': ' +

  The 'talk' behaviour is polymorphic because it is realised differently depending on the animal

  All of the above are valid

  Here, polymorphism allows us to specify common methods in an abstract level and implement them in instances (specifics)

  All animals 'talk'but they talk differently.

 9. Which of the following statements is true?
class Person(object):
    def pay_bill():
        raise NotImplementedError

class Millionare(Person):
    def pay_bill():
        print "Here you go! Keep the change!"

class GradStudent(Person):
    def pay_bill():
        print "Can I owe you ten bucks or do the dishes?"

  This demonstrates encapsulation as there are private attributes

  None of the above

  This demonstrates a form of polymorphism and inheritance is used to achieve it!

  This demonstrates polymorphic overloading and causes an error

 10. Polymorphism is very useful as it makes programming more intuitive and elegant.