250+ TOP MCQs on Python Classes and Objects and Answers

Python Multiple Choice Questions on “Classes and Objects – 1”.

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
Answer: b
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: d
Clarification: Instantiation refers to creating an object/instance for a class.

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
Answer: c
Clarification: 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
Answer: b
Clarification: Since the above code is being run not as a result of an import from another module, the variable will have value “__main__”.

Leave a Reply

Your email address will not be published. Required fields are marked *