Python MCQs on Classes and Objects: SET 1

Python MCQs on Classes and Objects

In this section, we are going to introduce you to the most frequently asked Python MCQs on Classes and Objects. These Python MCQs on Classes and Objects will be going to help you in Interviews and Exams.

 

1. _____ represents an entity in the real world with its identity and behaviour.

a) A method

b) An object

c) A class

d) An operator

Check Answer

Answer: b
An object represents an entity in the real world that can be distinctly identified. A class may define an object.

2. _____ is used to create an object.

a) class

b) constructor

c) User-defined functions

d) In-built functions

Check Answer

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

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

class test:

def __init__(self,a=”Hello World”):

self.a=a

def display(self):

print(self.a)

obj=test()

obj.display()

a) The program has an error because constructor can’t have default arguments

b) Nothing is displayed

c) “Hello World” is displayed

d) The program has an error display function doesn’t have parameters

Check Answer

Answer: c
The program has no error. “Hello World” is displayed. Execute in python shell to verify.

4. What is setattr() used for?

a) To access the attribute of the object

b) To set an attribute

c) To check if an attribute exists or not

d) To delete an attribute

Check Answer

Answer: b
setattr(obj,name,value) is used to set an attribute. If attribute doesn’t exist, then it would be created.

5. What is getattr() 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: a
getattr(obj,name) is used to get the attribute of an object.

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

class change:

def __init__(self, x, y, z):

self.a = x + y + z

x = change(1,2,3)

y = getattr(x, ‘a’)

setattr(x, ‘a’, y+1)

print(x.a)

a) 6

b) 7

c) Error

d) 0

Check Answer

Answer: b
First, a=1+2+3=6. Then, after setattr() is invoked, x.a=6+1=7.

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

class test:

def __init__(self,a):

self.a=a

def display(self):

print(self.a)

obj=test()

obj.display()

a) Runs normally, doesn’t display anything

b) Displays 0, which is the automatic default value

c) Error as one argument is required while creating the object

d) Error as display function requires additional argument

Check Answer

Answer: c
Since, the __init__ special method has another argument a other than self, during object creation, one argument is required. For example: obj=test(“Hello”)

8. Is the following Python code correct?

>>> class A:

def __init__(self,b):

self.b=b

def display(self):

print(self.b)

>>> obj=A(“Hello”)

>>> del obj

a) True

b) False

Check Answer

Answer: a
It is possible to delete an object of the class. On further typing obj in the python shell, it throws an error because the defined object has now been deleted.

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

class test:

def __init__(self):

self.variable = ‘Old’

self.Change(self.variable)

def Change(self, var):

var = ‘New’

obj=test()

print(obj.variable)

a) Error because function change can’t be called in the __init__ function

b) ‘New’ is printed

c) ‘Old’ is printed

d) Nothing is printed

Check Answer

Answer: c
This is because strings are immutable. Hence any change made isn’t reflected in the original string.

10. What is Instantiation in terms of OOP terminology?

a) Deleting an instance of class

b) Modifying an instance of class

c) Copying an instance of class

d) Creating an instance of class

Check Answer

Answer: d
Instantiation refers to creating an object/instance for a class.

 

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