Python MCQ’s on Polymorphism and Encapsulation: SET 2

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

class A:

def __init__(self):

self.multiply(15)

def multiply(self, i):

self.i = 4 * i;

class B(A):

def __init__(self):

super().__init__()

print(self.i)

def multiply(self, i):

self.i = 2 * i;

obj = B()

a) 15

b) 30

c) An exception is thrown

d) 60

Check Answer

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

12. 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: b
The method check is private so it can’t be accessed by the derived class. Execute the code in the Python shell.

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

class A:

def __init__(self, x, y):

self.x = x

self.y = y

def __str__(self):

return 1

def __eq__(self, other):

return self.x * self.y == other.x * other.y

obj1 = A(5, 2)

obj2 = A(2, 5)

print(obj1 == obj2)

a) False

b) 1

c) True

d) An exception is thrown

 

Check Answer

Answer: c
Since 5*2==2*5, True is printed. Execute it in the Python shell to verify.

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

class A:

def one(self):

return self.two()

def two(self):

return ‘A’

class B(A):

def two(self):

return ‘B’

obj2=B()

print(obj2.two())

a) A

b) An exception is thrown

c) A B

d) B

Check Answer

Answer: d
The derived class method two() overrides the method two() in the base class A.

15. Which of the following statements is true?

a) A non-private method in a superclass can be overridden

b) A subclass method can be overridden by the superclass

c) A private method in a superclass can be overridden

d) Overriding isn’t possible in Python

 

Check Answer

Answer: a
A public method in the base class can be overridden by the same named method in the subclass.

 

16. Which of these is not a fundamental features of OOP?

a) Encapsulation

b) Inheritance

c) Instantiation

d) Polymorphism

Check Answer

Answer: c
Instantiation simply refers to creation of an instance of class. It is not a fundamental feature of OOP.

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

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 implementation of elegant software that is well designed and easily modified

Check Answer

Answer: b
The values assigned by the constructor to the class members is used to create the object.

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

class Demo:

def __init__(self):

self.a = 1

self.__b = 1

def display(self):

return self.__b

obj = Demo()

print(obj.a)

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

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

c) The program runs fine and 1 is printed

d) The program has an error as you can’t name a class member using __b

Check Answer

Answer: c
The program has no error because the class member which is public is printed. 1 is displayed. Execute in python shell to verify.

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

class Demo:

def __init__(self):

self.a = 1

self.__b = 1

def display(self):

return self.__b

obj = Demo()

print(obj.__b)

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

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

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

d) The program runs fine and 1 is printed

Check Answer

Answer: c
Variables beginning with two underscores are said to be private members of the class and they can’t be accessed directly.

20. Methods of a class that provide access to private members of the class are called as ______ and ______

a) getters/setters

b) __repr__/__str__

c) user-defined functions/in-built functions

d) __init__/__del__

Check Answer

Answer: a
The purpose of getters and setters is to get(return) and set(assign) private instance variables of a class.

Page  1  2  3

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment