Python MCQs on Classes and Objects: SET 2

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

class fruits:

def __init__(self, price):

self.price = price

obj=fruits(50)

obj.quantity=10

obj.bags=2

print(obj.quantity+len(obj.__dict__))

a) 12

b) 52

c) 13

d) 60

Check Answer

Answer: c
In the above code, obj.quantity has been initialised to 10. There are a total of three items in the dictionary, price, quantity and bags. Hence, len(obj.__dict__) is 3.

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

class Demo:

def __init__(self):

pass

def test(self):

print(__name__)

obj = Demo()

obj.test()

a) Exception is thrown

b) __main__

c) Demo

d) test

Check Answer

Answer: b
Since the above code is being run not as a result of an import from another module, the variable will have value “__main__”.

13. The assignment of more than one function to a particular operator is _______

a) Operator over-assignment

b) Operator overriding

c) Operator overloading

d) Operator instance

Check Answer

Answer: c
The assignment of more than one function to a particular operator is called as operator overloading.

14. Which of the following is not a class method?

a) Non-static

b) Static

c) Bounded

d) Unbounded

Check Answer

Answer: a
The three different class methods in Python are static, bounded and unbounded methods.

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

def add(c,k):

c.test=c.test+1

k=k+1

class A:

def __init__(self):

self.test = 0

def main():

Count=A()

k=0

for i in range(0,25):

add(Count,k)

print(“Count.test=”, Count.test)

print(“k =”, k)

main()

a) Exception is thrown

b)

Count.test=25
k=25

c)

Count.test=25
k=0

d)

Count.test=0
k=0

Check Answer

Answer: c
The program has no error. Here, test is a member of the class while k isn’t. Hence test keeps getting incremented 25 time while k remains 0.

16. Which of the following Python code creates an empty class?

a)

class A:
return

b)

class A:
pass

c)

class A:

d) It is not possible to create an empty class

Check Answer

Answer: b
Execute in python shell to verify.

17. Is the following Python code valid?

class B(object):

def first(self):

print(“First method called”)

def second():

print(“Second method called”)

ob = B()

B.first(ob)

a) It isn’t as the object declaration isn’t right

b) It isn’t as there isn’t any __init__ method for initializing class members

c) Yes, this method of calling is called unbounded method call

d) Yes, this method of calling is called bounded method call

Check Answer

Answer: c
The method may be created in the method demonstrated in the code as well and this is called as the unbounded method call. Calling the method using obj.one() is the bounded method call.

18. What are the methods which begin and end with two underscore characters called?

a) Special methods

b) In-built methods

c) User-defined methods

d) Additional methods

Check Answer

Answer: a
Special methods like __init__ begin and end with two underscore characters.

19. Special methods need to be explicitly called during object creation.

a) True

b) False

Check Answer

Answer: b
Special methods are automatically called during object creation.

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

>>> class demo():

def __repr__(self):

return ‘__repr__ built-in function called’

def __str__(self):

return ‘__str__ built-in function called’

>>> s=demo()

>>> print(s)

a) Error

b) Nothing is printed

c) __str__ called

d) __repr__ called

Check Answer

Answer: c
__str__ is used for producing a string representation of an object’s value that Python can evaluate. Execute in python shell to verify.

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

>>> class demo():

def __repr__(self):

return ‘__repr__ built-in function called’

def __str__(self):

return ‘__str__ built-in function called’

>>> s=demo()

>>> print(s)

a) __str__ called

b) __repr__ called

c) Error

d) Nothing is printed

Check Answer

Answer: a
__str__ is used for producing a string representation of an object’s value that is most readable for humans. Execute in python shell to verify.

22. What is hasattr(obj,name) used for?

a) To access the attribute of the object

b) To delete an attribute

c) To check if an attribute exists or not

d) To set an attribute

Check Answer

Answer: c
hasattr(obj,name) checks if an attribute exists or not and returns True or False.

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

class stud:

def __init__(self, roll_no, grade):

self.roll_no = roll_no

self.grade = grade

def display (self):

print(“Roll no : “, self.roll_no, “, Grade: “, self.grade)

stud1 = stud(34, ‘S’)

stud1.age=7

print(hasattr(stud1, ‘age’))

a) Error as age isn’t defined

b) True

c) False

d) 7

Check Answer

Answer: b
Execute in python shell to verify.

24. What is delattr(obj,name) used for?

a) To print deleted attribute

b) To delete an attribute

c) To check if an attribute is deleted or not

d) To set an attribute

Check Answer

Answer: b
delattr(obj,name) deletes an attribute in a class.

25. __del__ method is used to destroy instances of a class.

a) True

b) False

Check Answer

Answer: a
___del__ method acts as a destructor and is used to destroy objects of classes.

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

class stud:

‘Base class for all students’

def __init__(self, roll_no, grade):

self.roll_no = roll_no

self.grade = grade

def display (self):

print(“Roll no : “, self.roll_no, “, Grade: “, self.grade)

print(student.__doc__)

a) Exception is thrown

b) __main__

c) Nothing is displayed

d) Base class for all students

Check Answer

Answer: d
___doc__ built-in class attribute is used to print the class documentation string or none, if undefined.

27. What does print(Test.__name__) display (assuming Test is the name of the class)?

a) ()

b) Exception is thrown

c) Test

d) __main__

Check Answer

Answer: c
__name__ built-in class attribute is used to display the class name.

Page  1  2

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