Python OOPS MCQ
1). Which of the following assertions most accurately sums up Python’s encapsulation?
a) It is a process of hiding the implementation details of a class.
b) It is a process of creating multiple instances of a class.
c) It is a process of defining attributes and methods in a class.
d) It is a process of converting data types in Python.
Correct answer is: a) It is a process of hiding the implementation details of a class.
Explanation: Encapsulation in Python refers to the bundling of data and methods within a class, allowing the implementation details to be hidden from the outside world.
2). Which of the following is an example of inheritance in Python?
a) A class inheriting properties from another class.
b) A class implementing an interface.
c) A class containing multiple methods.
d) A class accessing variables from a module.
Correct answer is: a) A class inheriting properties from another class.
Explanation: A fundamental aspect of object-oriented programming is inheritance. It allows a class to inherit attributes and methods from another class, known as the superclass or base class.
3). What is the purpose of the `super()` function in Python?
a) It is used to invoke a superclass’s method.
b) It is used to define class attributes.
c) It is used to create an instance of a class.
d) It is used to encapsulate class methods.
Correct answer is: a) It is used to invoke a superclass’s method.
Explanation: The `super()` function is used to call a method or access an attribute from the superclass within a subclass.
4). Which of the following is true about multiple inheritance in Python?
a) A class may descended from various superclasses.
b) Multiple classes can inherit from a single superclass.
c) A class can inherit from only one superclass.
d) Multiple classes cannot be used together in Python.
Correct answer is: a) A class may descended from various superclasses.
Explanation: Python supports multiple inheritance, which means a class can inherit attributes and methods from multiple parent classes.
5). What is the purpose of the `__init__` method in Python classes?
a) It is a special method used to create a new instance of a class.
b) It is used to define class attributes.
c) It is a built-in method that Python automatically calls.
d) It is used to delete an instance of a class.
Correct answer is: a) It is a special method used to create a new instance of a class.
Explanation: The `__init__` method, also known as the constructor, is called when a new instance of a class is created. The characteristics of the object are initialised using it.
6). What is the purpose of the `self` parameter in Python class methods?
a) It represents the superclass of a class.
b) It represents the current instance of a class.
c) It is a keyword used to define class attributes.
d) It is a built-in function in Python.
Correct answer is: b) It represents the current instance of a class.
Explanation: The `self` parameter is a reference to the current instance of a class. It is used to access and modify the object’s attributes and methods.
7). Which of the following best describes polymorphism in Python?
a) It allows a class to inherit from multiple superclasses.
b) It allows a class to hide its implementation details.
c) It allows different classes to implement the same method name.
d) It allows a class to access variables from a module.
Correct answer is: c) It allows different classes to implement the same method name.
Explanation: Polymorphism in Python refers to the ability of different classes to define the same method name but with different implementations.
8). What is method overriding in Python?
a) It allows a subclass to inherit attributes from a superclass.
b) It allows a subclass to implement its own version of a method inherited from a superclass.
c) It allows a subclass to access the private attributes of a superclass.
d) It allows a subclass to hide its implementation details.
Correct answer is: b) It allows a subclass to implement its own version of a method inherited from a superclass.
Explanation: The act of a subclass providing its own implementation of a method that is already defined in its superclass is known as method overriding.
9). What is the difference between class variables and instance variables in Python?
a) Class variables are defined within methods, while instance variables are defined outside methods.
b) Class variables are accessible only within the class, while instance variables are accessible outside the class.
c) Class variables have a different value for each instance of a class, while instance variables have the same value for all instances.
d) Class variables are used to define attributes, while instance variables are used to define methods.
Correct answer is: c) Class variables have a different value for each instance of a class, while instance variables have the same value for all instances.
Explanation: Class variables are shared among all instances of a class and have the same value, whereas instance variables have distinct values for each instance of a class.
10). Which of the following is an example of a composition relationship in Python?
a) A class inheriting properties from another class.
b) A class having an instance of another class as its attribute.
c) A class implementing an interface.
d) A class accessing variables from a module.
Correct answer is: b) A class having an instance of another class as its attribute.
Explanation: Composition is a relationship where a class contains an instance of another class as one of its attributes.
11). What is the difference between a class method and an instance method in Python?
a) Class methods can access instance attributes, while instance methods cannot.
b) Instance methods can access class attributes, while class methods cannot.
c) Class methods are defined using the `@classmethod` decorator, while instance methods are not.
d) Instance methods are called using the class name, while class methods are called using an instance of the class.
Correct answer is: b) Instance methods can access class attributes, while class methods cannot.
Explanation: Instance methods have access to both instance and class attributes, while class methods can only access class attributes.
12). What is the purpose of the `@staticmethod` decorator in Python?
a) It allows a method to be called without creating an instance of the class.
b) It converts a class method into an instance method.
c) It enables inheritance between classes.
d) It defines a method within a class.
Correct answer is: a) It allows a method to be called without creating an instance of the class.
Explanation: The `@staticmethod` decorator is used to define a static method in a class. Without creating an instance, static methods can be called on the class itself.
13). What is the role of an abstract base class (ABC) in Python?
a) It allows multiple inheritance.
b) It defines a base class that cannot be instantiated and provides a common interface for its subclasses.
c) It allows private access to class attributes.
d) It provides a mechanism for encapsulation.
Correct answer is: b) It defines a base class that cannot be instantiated and provides a common interface for its subclasses.
Explanation: An abstract base class in Python provides a common interface for its subclasses. It acts as a model for its subclasses but cannot be instantiated.
14). Which keyword is used to access the superclass from within a subclass in Python?
a) super
b) self
c) parent
d) base
Correct answer is: a) super
Explanation: The `super` keyword is used to access the superclass from within a subclass. It allows the subclass to invoke methods or access attributes of the superclass.
15). What is the purpose of the `__str__` method in Python classes?
a) It converts a class to a string representation.
b) It initializes the attributes of a class.
c) It defines the equality between two instances of a class.
d) It creates a deep copy of an instance.
Correct answer is: a) It converts a class to a string representation.
Explanation: The `__str__` method is used to define the string representation of a class. It is automatically called when the `str()` function is used on an instance of the class.
16). What is the difference between shallow copy and deep copy in Python?
a) Shallow copy copies only the references, while deep copy copies the actual values.
b) Shallow copy creates a new object, while deep copy creates a reference to the original object.
c) Shallow copy creates an independent copy of an object, while deep copy creates a nested copy.
d) Shallow copy only works for immutable objects, while deep copy works for both mutable and immutable objects.
Correct answer is: a) Shallow copy copies only the references, while deep copy copies the actual values.
Explanation: Deep copy generates a completely independent copy with its own values, whereas shallow copy creates a new object that references the values of the original object.
17). Which of the following is true about the `__del__` method in Python?
a) It is used to define class attributes.
b) It is automatically invoked whenever a class instance is generated.
c) It is a destructor method that is called when an instance is about to be destroyed.
d) It is used to initialize the attributes of a class.
Correct answer is: c) It is a destructor method that is called when an instance is about to be destroyed.
Explanation: The `__del__` method is a destructor method in Python. It is automatically called when an instance of a class is about to be destroyed.
18). What is the purpose of method overloading in Python?
a) It allows a method to have different implementations based on the number and types of arguments.
b) It allows a method to override another method from a superclass.
c) It allows a method to be called without passing any arguments.
d) It allows a method to be called from within another method.
Correct answer is: a) It allows a method to have different implementations based on the number and types of arguments.
Explanation: Method overloading in Python allows a method to have multiple implementations with different numbers and types of arguments.
19). Which of the following statements is true about method overriding in Python?
a) A subclass can override a method in its superclass.
b) A subclass can only override a private method in its superclass.
c) A subclass can override a method in any other class.
d) A subclass cannot override a method in its superclass.
Correct answer is: a) A subclass can override a method in its superclass.
Explanation: A subclass can offer its own implementation of a method that is already defined in its superclass by using method overriding.
20). What is the purpose of the `@property` decorator in Python?
a) It is used to define a class attribute.
b) It is used to define a static method.
c) It is used to create a read-only property with a getter method.
d) It is used to create a write-only property with a setter method.
Correct answer is: c) It is used to create a read-only property with a getter method.
Explanation: The `@property` decorator is used to define a getter method for creating a read-only property in a class.
21). What is abstraction?
a) The ability of an object to take on different forms
b) The process of one class inheriting the properties and methods of another class
c) The process of hiding the implementation details of an object from the user
d) The process of representing abstract concepts
Correct answer is: d) The process of representing abstract concepts
Explanation: Abstraction is one of the most important features of object-oriented programming. It allows for the creation of objects that represent things that do not have a physical representation in the real world. For example, a class can be used to represent the concept of a “person”, even though there is no such thing as a “person” in the real world.
22). What distinguishes a constructor from a method?
a) A constructor is a method that is called when an object of that class is created, while a method is a function that is associated with a class
b) A constructor is a method that is used to initialize the data members of an object, while a method is a function that is used to perform operations on an object
c) A constructor is a method that is called when an object of that class is deleted, while a method is a function that is called when an object of that class is modified
d) None of the above
Correct answer is: b) A constructor is a method that is used to initialize the data members of an object, while a method is a function that is used to perform operations on an object