Python Programming Multiple Choice Question -
Classes and Objects
This section focuses on the "Classes and Objects" in Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. To create a class, use the keyword?
A. new
B. except
C. class
D. object
View Answer
Ans : C
Explanation: To create a class, use the keyword class
2. All classes have a function called?
A. __init__
B. __init__()
C. init
D. init()
View Answer
Ans : B
Explanation: All classes have a function called __init__(), which is always executed when the class is being initiated.
3. The __________ parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
A. __init__()
B. self
C. both A and B
D. None of the above
View Answer
Ans : B
Explanation: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
4. You can delete properties on objects by using the ______ keyword.
A. delete
B. dedl
C. del
D. drop
View Answer
Ans : C
Explanation: You can delete properties on objects by using the del keyword
5. A variable that is defined inside a method and belongs only to the current instance of a class is known as?
A. Inheritance
B. Instance variable
C. Function overloading
D. Instantiation
View Answer
Ans : B
Explanation: Instance variable : A variable that is defined inside a method and belongs only to the current instance of a class.
6. A class variable or instance variable that holds data associated with a class and its object is known as?
A. Class variable
B. Method
C. Operator overloading
D. Data member
View Answer
Ans : D
Explanation: Data member : A class variable or instance variable that holds data associated with a class and its objects.
7. What is setattr() used for?
A. To set an attribute
B. To access the attribute of the object
C. To check if an attribute exists or not
D. To delete an attribute
View Answer
Ans : A
Explanation: setattr(obj,name,value) is used to set an attribute. If attribute doesn’t exist, then it would be created.
8. What will be output for the folllowing 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
Ans : 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”)
9. _____ 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
Ans : B
Explanation: An object represents an entity in the real world that can be distinctly identified. A class may define an object.
10. The class has a documentation string, which can be accessed via?
A. ClassName
B. ClassName __doc__
C. __doc__
D. ClassName.__doc__
View Answer
Ans : D
Explanation: The class has a documentation string, which can be accessed via ClassName.__doc__.
Discussion