Preview

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

  class

  method

  value

 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 a static object that never changes

  None of the above

  define the area method so that it remains the same

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

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

  a single form to avoid corruption

  polymath format (e.g numeric format)

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

  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 _____________

  encapsulation

  polymorphism when applied only to numbers

  the polymorphic property of overriding

  None of the above

 5. What is the use of duck typing?

  No 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

  Makes the program code smaller

  More 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.

  polymorphic overloading

  None of the above

  duck typing

  overriding

 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):
        super().__init__()
class C(B):
    def __init__(self):
        super().__init__()
def main():
    obj1 = B()
    obj2 = A()
    obj3 = C()
    print(obj1, obj2,obj3)
main()

  TRUE

  FALSE

 8. How is the following example demonstrating polymorphism?
class Animal:
    def __init__(self, name):    # Constructor of the class
        self.name = 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'),
           Dog('Lassie')]

for animal in animals:
    print (animal.name + ': ' + animal.talk())

  All animals 'talk'but they talk differently.

  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)

 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 polymorphic overloading and causes an error

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

  This demonstrates encapsulation as there are private attributes

  None of the above

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

  TRUE

  FALSE