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
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
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?
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
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
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
Explanation: 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
View Answer
Explanation: 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
View Answer
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
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
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
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
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
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.
- Apply for Python Internship
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs
- Check Python Books