Python Questions and Answers – Classes and Objects – 1

This set of Python Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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?

advertisement
advertisement
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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: getattr(obj,name) is used to get the attribute of an object.
advertisement

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
View Answer

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

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.