Here are 30 multiple-choice questions (MCQs) on Object-Oriented Programming (OOP) in Python, divided by topics:
1. Classes and Objects
- Which of the following is the correct way to define a class in Python?
- a)
class MyClass{}
- b)
class MyClass:
- c)
MyClass = class()
- d)
def class MyClass:
- To create an object of a class, you use the following syntax:
- a)
obj = MyClass{}
- b)
obj = MyClass()
- c)
obj = new MyClass()
- d)
obj = create MyClass()
- What is the purpose of the
__init__ method in a class?
- a) To initialize the class attributes
- b) To initialize the object attributes
- c) To create a new class
- d) To define the class method
- In Python, which keyword is used to refer to the current instance of a class?
- a)
self
- b)
this
- c)
cls
- d)
object
2. Instance and Class Variables
- Which of the following is an example of an instance variable?
- a)
self.name
- b)
MyClass.name
- c)
cls.name
- d)
name
- Which of the following is an example of a class variable?
- a)
self.age
- b)
cls.age
- c)
age
- d)
obj.age
- What is the main difference between instance variables and class variables?
- a) Instance variables are shared across all instances of a class, class variables are not.
- b) Instance variables belong to an instance, class variables belong to the class.
- c) Instance variables are constant, class variables are mutable.
- d) Instance variables are private, class variables are public.
3. Methods: Instance, Class, and Static Methods
- Which method is used to access an instance’s attributes in Python?
- a) Instance method
- b) Class method
- c) Static method
- d) All of the above
- What is the difference between a class method and a static method in Python?
- a) A class method modifies class variables, while a static method does not access class variables.
- b) A class method is called on an object, whereas a static method is called on the class.
- c) A static method can modify instance attributes, while a class method cannot.
- d) There is no difference.
- Which decorator is used to define a class method?
- a)
@staticmethod
- b)
@classmethod
- c)
@instance
- d)
@classmethods
- What does the
self parameter in an instance method represent?
- a) The class itself
- b) The current instance of the class
- c) A global variable
- d) The method’s return value
4. Constructor (init)
- Which of the following statements is true about the
__init__ method?
- a) It is automatically invoked when an object is created.
- b) It is used to define the class attributes.
- c) It is only called once during the lifetime of a class.
- d) It must return a value.
- What is the purpose of the
__init__ method?
- a) To initialize class attributes.
- b) To initialize instance attributes.
- c) To create an object of the class.
- d) To execute the main logic of the class.
5. Inheritance
- Which of the following defines inheritance in Python?
- a) A way to reuse methods and properties of a class in another class.
- b) A way to modify instance variables in a class.
- c) A way to execute code when a method is called.
- d) A way to hide the details of a class.
- How do you define a class that inherits from another class in Python?
- a)
class ChildClass:
- b)
class ChildClass(baseClass):
- c)
class ChildClass inherits baseClass:
- d)
class ChildClass extends baseClass:
- Which keyword is used to call the constructor of a base class in a derived class?
- a)
super()
- b)
this()
- c)
parent()
- d)
base()
6. Polymorphism
- What is polymorphism in Python?
- a) The ability to define methods with the same name in different classes.
- b) The ability of an object to take on many forms.
- c) The ability to inherit methods from another class.
- d) The ability to create a constructor.
- Which of the following is an example of polymorphism?
- a) A class that inherits from multiple classes.
- b) A method in one class overriding a method in another class.
- c) A class that has multiple constructors.
- d) A method that changes its behavior based on the number of arguments.
- Which of the following is an example of method overriding?
- a) A subclass defines a method with the same name as the base class.
- b) A subclass defines a constructor for a base class.
- c) A class method overrides a static method.
- d) A class method uses a super class method.
7. Encapsulation
- What is encapsulation in Python?
- a) Hiding the internal state of an object and requiring all interaction to be performed through methods.
- b) Inheriting properties from a base class.
- c) Defining a class that can handle different types of data.
- d) Creating objects from classes.
- Which of the following helps to implement encapsulation in Python?
- a) Using public attributes.
- b) Using private attributes.
- c) Using global variables.
- d) Using inheritance.
- How do you make an attribute private in Python?
- a) By prefixing it with a double underscore
__.
- b) By making it a class variable.
- c) By using
private keyword.
- d) By using
protected keyword.
8. Abstraction
- What is abstraction in Python?
- a) Hiding the complexity of implementation and showing only essential features of an object.
- b) The ability to modify an object’s state.
- c) The process of adding new behavior to a class.
- d) Inheriting from multiple classes.
- Which of the following Python modules helps in achieving abstraction?
- a)
abstract
- b)
abc
- c)
abstractmethod
- d) All of the above
- Which of the following is a benefit of abstraction?
- a) Simplifies the program by hiding unnecessary details.
- b) Allows for direct manipulation of an object’s state.
- c) Increases the program’s complexity.
- d) Provides better memory management.
9. Method Overriding
- What is method overriding in Python?
- a) Redefining a method in a subclass that was already defined in the parent class.
- b) Defining a method with different parameters in a subclass.
- c) Defining a new class method in the parent class.
- d) Hiding a method from the base class.
- In Python, how is method overriding achieved?
- a) By using the
@staticmethod decorator.
- b) By redefining the method in the child class with the same name.
- c) By using the
super() function.
- d) By calling the parent class method.
10. Operator Overloading
- What is operator overloading in Python?
- a) Defining custom behavior for operators on objects.
- b) Defining new methods for operators.
- c) Creating multiple constructors for a class.
- d) Changing the order of method execution.
- Which of the following operators can be overloaded in Python?
- a)
+
- b)
-
- c)
*
- d) All of the above
- How do you overload the addition operator in Python?
- a) By defining the
__add__ method in the class.
- b) By defining the
__sum__ method in the class.
- c) By defining the
__operator__ method in the class.
- d) By using the
@operator decorator.
Answers (Tabular Form)
| Qno | Answer |
|---|
| 1 | b) class MyClass: |
| 2 | b) obj = MyClass() |
| 3 | b) To initialize the object attributes |
| 4 | a) self |
| 5 | a) self.name |
| 6 | b) cls.age |
| 7 | b) Instance variables belong to an instance, class variables belong to the class. |
| 8 | a) Instance method |
| 9 | a) A class method modifies class variables, while a static method does not access class variables. |
| 10 | b) @classmethod |
| 11 | b) The current instance of the class |
| 12 | a) It is automatically invoked when an object is created. |
| 13 | b) To initialize instance attributes. |
| 14 | a) A way to reuse methods and properties of a class in another class. |
| 15 | b) class ChildClass(baseClass): |
| 16 | a) super() |
| 17 | b) The ability of an object to take on many forms. |
| 18 | b) A method in one class overriding a method in another class. |
| 19 | a) A subclass defines a method with the same name as the base class. |
| 20 | a) Hiding the internal state of an object and requiring all interaction to be performed through methods. |
| 21 | b) Using private attributes. |
| 22 | a) By prefixing it with a double underscore __. |
| 23 | a) Hiding the complexity of implementation and showing only essential features of an object. |
| 24 | d) All of the above |
| 25 | a) Simplifies the program by hiding unnecessary details. |
| 26 | a) Redefining a method in a subclass that was already defined in the parent class. |
| 27 | b) By redefining the method in the child class with the same name. |
| 28 | a) Defining custom behavior for operators on objects. |
| 29 | d) All of the above |
| 30 | a) By defining the __add__ method in the class. |
Post Views: 57