Preview

05-Polymorphism and Encapsulation

 1. Polymorphism can be described in many ways: One explanation is that it is

  a property of OOP that supports morphing and polying around

  a property of OOP that allows an object to inherit many forms of a class

  a property of OOP that allows polymathematical operations to occur

  a property of OOP that allows the programmer to make a program accept any data that they want into a method and it will be able to cope with it.

 2. An example of polymorphism would be: if we had a base class called shape, our area method should be able to accept circles, triangles etc and the area should still be calculated correctly. This usually uses a process called:

  'Overimplementing'

  'Polycacheing'

  â€˜Overloading’

  'Encapsulation'

 3. What is the output of the following code?
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()

  1,2,3

  1,1,0

  1,1,1

  An exception is thrown

 4. What is the output from the following code?
class A:
    def __repr__(self):
        return "1"
class B(A):
    def __repr__(self):
        return "2"
class C(B):
    def __repr__(self):
        return "3"
o1 = A()
o2 = B()
o3 = C()
print(obj1, obj2, obj3)

  Error

  1,1,1

  1,2,3

  0,0,0

 5. Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are executed.

  False

  True

 6. What is the output of the following code?
class A:
    def __init__(self, i = 2, j = 3):
        self.i = i
        self.j = j

    def __str__(self):
        return "A"

    def __eq__(self, other):
        return self.i * self.j == other.i * other.j

def main():
    x = A(1, 2)
    y = A(2, 1)
    print(x == y)

main()

  False

  True

  2

  1

 7. Which of the following is the most suitable definition for encapsulation?

  Ability of a class to derive members of another class as a part of its own definition

  Means of bundling instance variables and methods in order to restrict access to certain class members

  Focuses on variables and passing of variables to functions

  Allows for implementation of elegant software that is well designed

 8. Methods of a class that provide access to private members of the class are sometimes referred to as ______ and ______. Their purpose is to get(return) and set(assign) private instance variables of a class.

  getters/setters

  user-defined functions/in-built functions

  __init__/__del__

  __repr__/__str__

 9. Which of these is a "private" data field?
def Demo:
def __init__(self):
    __a = 1
    self.__b = 1
    self.__c__ = 1
    __d__= 1

  __b

  __a

  __c__

  __d__

 10. What is the output of the following code?
class Demo:
     def __init__(self):
         self.a = 1
         self.__b = 1
 
     def get(self):
         return self.__b
 
obj = Demo()
print(obj.get())

  The program runs fine and 1 is printed

  The program has an error because b is private and display(self) is returning a private member

  The program has an error because there isn’t any function to return self.a

  The program has an error because b is private and hence can’t be printed