Python MCQ’s on Polymorphism and Encapsulation: SET 1

1. Which of the following best describes polymorphism?

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

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

c) Focuses on variables and passing of variables to functions

d) Allows for objects of different types and behaviour to be treated as the same general type

Check Answer

Answer: d
Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified.

2. What is the biggest reason for the use of polymorphism?

a) It allows the programmer to think at a more abstract level

b) There is less program code to write

c) The program will have a more elegant design and will be easier to maintain and update

d) Program code takes up less space

Check Answer

Answer: c
Polymorphism allows for the implementation of elegant software.

3. What is the use of duck typing?

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

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

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

d) Makes the program code smaller

Check Answer

Answer: c
In Python, any set of classes with a common set of methods can be treated similarly. This is called duck typing. Hence duck typing imposes less restrictions.

4. What will be the output of the following Python 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()

a) 1 1 1

b) 1 2 3

c) ‘1’ ‘1’ ‘1’

d) An exception is thrown

 

Check Answer

Answer: a
The super().__init__() in the subclasses has been properly invoked and none of other subclasses return any other value. Hence 1 is returned each time the object is created and printed.

5. What will be the output of the following Python code?

class Demo:

def __init__(self):

self.x = 1

def change(self):

self.x = 10

class Demo_derived(Demo):

def change(self):

self.x=self.x+1

return self.x

def main():

obj = Demo_derived()

print(obj.change())

main()

a) 11

b) 2

c) 1

d) An exception is thrown

Check Answer

Answer: b
The derived class method change() overrides the base class method.

6. A class in which one or more methods are only implemented to raise an exception is called an abstract class.

a) True

b) False

Check Answer

Answer: a
A class in which one or more methods are unimplemented or implemented for the methods throw an exception is called an abstract class.

7. Overriding means changing behaviour of methods of derived class methods in the base class.

a) True

b) False

Check Answer

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

8. What will be the output of the following Python 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)

a) 1 1 1

b) 1 2 3

c) ‘1’ ‘1’ ‘1’

d) An exception is thrown

 

Check Answer

Answer: b
When different objects are invoked, each of the individual classes return their individual values and hence it is printed.

9. What will be the output of the following Python code?

class A:

def __init__(self):

self.multiply(15)

print(self.i)

def multiply(self, i):

self.i = 4 * i;

class B(A):

def __init__(self):

super().__init__()

def multiply(self, i):

self.i = 2 * i;

obj = B()

a) 15

b) 60

c) An exception is thrown

d) 30

Check Answer

Answer: d
The derived class B overrides base class A.

10. What will be the output of the following Python code?

class Demo:

def check(self):

return ” Demo’s check “

def display(self):

print(self.check())

class Demo_Derived(Demo):

def check(self):

return ” Derived’s check “

Demo().display()

Demo_Derived().display()

a) Demo’s check Derived’s check

b) Demo’s check Demo’s check

c) Derived’s check Demo’s check

d) Syntax error

Check Answer

Answer: a
Demo().display() invokes the display() method in class Demo and Demo_Derived().display() invokes the display() method in class Demo_Derived.

Page  1  2  3

Leave a Comment