MCQs on Object-Oriented Programming (OOP) | Python programming

Here are 30 multiple-choice questions (MCQs) on Object-Oriented Programming (OOP) in Python, divided by topics:


1. Classes and Objects

  1. 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:
  2. 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()
  3. 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
  4. 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

  1. Which of the following is an example of an instance variable?
    • a) self.name
    • b) MyClass.name
    • c) cls.name
    • d) name
  2. Which of the following is an example of a class variable?
    • a) self.age
    • b) cls.age
    • c) age
    • d) obj.age
  3. 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

  1. 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
  2. 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.
  3. Which decorator is used to define a class method?
    • a) @staticmethod
    • b) @classmethod
    • c) @instance
    • d) @classmethods
  4. 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)

  1. 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.
  2. 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

  1. 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.
  2. 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:
  3. 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

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. Which of the following Python modules helps in achieving abstraction?
    • a) abstract
    • b) abc
    • c) abstractmethod
    • d) All of the above
  3. 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

  1. 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.
  2. 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

  1. 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.
  2. Which of the following operators can be overloaded in Python?
    • a) +
    • b) -
    • c) *
    • d) All of the above
  3. 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)

QnoAnswer
1b) class MyClass:
2b) obj = MyClass()
3b) To initialize the object attributes
4a) self
5a) self.name
6b) cls.age
7b) Instance variables belong to an instance, class variables belong to the class.
8a) Instance method
9a) A class method modifies class variables, while a static method does not access class variables.
10b) @classmethod
11b) The current instance of the class
12a) It is automatically invoked when an object is created.
13b) To initialize instance attributes.
14a) A way to reuse methods and properties of a class in another class.
15b) class ChildClass(baseClass):
16a) super()
17b) The ability of an object to take on many forms.
18b) A method in one class overriding a method in another class.
19a) A subclass defines a method with the same name as the base class.
20a) Hiding the internal state of an object and requiring all interaction to be performed through methods.
21b) Using private attributes.
22a) By prefixing it with a double underscore __.
23a) Hiding the complexity of implementation and showing only essential features of an object.
24d) All of the above
25a) Simplifies the program by hiding unnecessary details.
26a) Redefining a method in a subclass that was already defined in the parent class.
27b) By redefining the method in the child class with the same name.
28a) Defining custom behavior for operators on objects.
29d) All of the above
30a) By defining the __add__ method in the class.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top