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 __________

  method

  class

  value

  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 _____________

  encapsulation

  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

  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()

  FALSE

  TRUE

 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())

  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.

  TRUE

  FALSE