Python MCQs on Polymorphism and Encapsulation: SET 3

21. Which of these is a private data field?

def Demo:

def __init__(self):

__a = 1

self.__b = 1

self.__c__ = 1

__d__= 1

a) __a

b) __b

c) __c__

d) __d__

Check Answer

Answer: b
Variables such as self.__b are private members of the class.

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

class Demo:

def __init__(self):

self.a = 1

self.__b = 1

def get(self):

return self.__b

obj = Demo()

print(obj.get())

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: d
Here, get(self) is a member of the class. Hence, it can even return a private member of the class. Because of this reason, the program runs fine and 1 is printed.

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

class Demo:

def __init__(self):

self.a = 1

self.__b = 1

def get(self):

return self.__b

obj = Demo()

obj.a=45

print(obj.a)

a) The program runs properly and prints 45

b) The program has an error because the value of members of a class can’t be changed from outside the class

c) The program runs properly and prints 1

d) The program has an error because the value of members outside a class can only be changed as self.a=45

Check Answer

Answer: a
It is possible to change the values of public class members using the object of the class.

24. Private members of a class cannot be accessed.

a) True

b) False

Check Answer

Answer: b
Private members of a class are accessible if written as follows: obj._Classname__privatemember. Such renaming of identifiers is called as name mangling.

25. The purpose of name mangling is to avoid unintentional access of private class members.

a) True

b) False

Check Answer

Answer: a
Name mangling prevents unintentional access of private members of a class, while still allowing access when needed. Unless the variable is accessed with its mangled name, it will not be found.

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

class fruits:

def __init__(self):

self.price = 100

self.__bags = 5

def display(self):

print(self.__bags)

obj=fruits()

obj.display()

a) The program has an error because display() is trying to print a private class member

b) The program runs fine but nothing is printed

c) The program runs fine and 5 is printed

d) The program has an error because display() can’t be accessed

Check Answer

Answer: c
Private class members can be printed by methods which are members of the class.

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

class student:

def __init__(self):

self.marks = 97

self.__cgpa = 8.7

def display(self):

print(self.marks)

obj=student()

print(obj._student__cgpa)

a) The program runs fine and 8.7 is printed

b) Error because private class members can’t be accessed

c) Error because the proper syntax for name mangling hasn’t been implemented

d) The program runs fine but nothing is printed

Check Answer

Answer: a
Name mangling has been properly implemented in the code given above and hence the program runs properly.

28. Which of the following is false about protected class members?

a) They begin with one underscore

b) They can be accessed by subclasses

c) They can be accessed by name mangling method

d) They can be accessed within a class

Check Answer

Answer: c
Protected class members can’t be accessed by name mangling.

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

class objects:

def __init__(self):

self.colour = None

self._shape = “Circle”

def display(self, s):

self._shape = s

obj=objects()

print(obj._objects_shape)

a) The program runs fine because name mangling has been properly implemented

b) Error because the member shape is a protected member

c) Error because the proper syntax for name mangling hasn’t been implemented

d) Error because the member shape is a private member

Check Answer

Answer: b
Protected members begin with one underscore and they can only be accessed within a class or by subclasses.

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