Python OOPS MCQ : Set 2

Python OOPS MCQ

1). What is the purpose of the `@classmethod` 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 `@classmethod` decorator is used to define a class method in Python. There is no need to create an instance when using class methods on the class itself.

2). What is the difference between public, protected, and private access modifiers in Python?

a) Public attributes are accessible within the class, protected attributes are accessible within the class and its subclasses, and private attributes are accessible only within the class.
b) Public attributes are accessible only within the class, protected attributes are accessible within the class and its subclasses, and private attributes are accessible within the module.
c) Public attributes are accessible within the class and its subclasses, protected attributes are accessible within the module, and private attributes are accessible only within the class.
d) Public attributes are accessible within the class and its subclasses, protected attributes are accessible within the module, and private attributes are accessible within the class.

Correct answer is: a) Public attributes are accessible within the class, protected attributes are accessible within the class and its subclasses, and private attributes are accessible only within the class.
Explanation: In Python, attributes and methods can have different access modifiers. Public attributes are accessible within the class, protected attributes are accessible within the class and its subclasses, and private attributes are accessible only within the class.

3). What is the purpose of the `__slots__` attribute in Python classes?
a) It defines the class attributes.
b) It restricts the creation of instance attributes to a predefined set.
c) It specifies the superclass of a class.
d) It enables multiple inheritance.

Correct answer is: b) It restricts the creation of instance attributes to a predefined set.
Explanation: The `__slots__` attribute in Python classes restricts the creation of instance attributes to a specific set of names. It can optimize memory usage by avoiding the creation of a dynamic dictionary for every instance.

4). Which of the following is true about method resolution order (MRO) in Python?

a) MRO defines the order in which methods are inherited from multiple superclasses.
b) MRO determines the access level of methods and attributes.
c) MRO specifies the number of methods that can be defined in a class.
d) MRO determines the lifespan of an instance.

Correct answer is: a) MRO defines the order in which methods are inherited from multiple superclasses.
Explanation: Method resolution order (MRO) in Python defines the order in which methods are inherited from multiple superclasses. It is determined using the C3 linearization algorithm.

5). What is the purpose of the `__new__` method in Python?

a) It is used to define class attributes.
b) It is automatically called when an instance is about to be destroyed.
c) It is used to create and initialize a new instance of a class.
d) It is used to delete an instance of a class.

Correct answer is: c) It is used to create and initialize a new instance of a class.
Explanation: The `__new__` method in Python is responsible for creating and initializing a new instance of a class. It is called before the `__init__` method.

6). What is the difference between an instance attribute and a class attribute in Python?

a) Instance attributes are defined within methods, while class attributes are defined outside methods.
b) Instance attributes are accessible only within the class, while class attributes are accessible outside the class.
c) Instance attributes have a different value for each instance of a class, while class attributes have the same value for all instances.
d) Instance attributes are used to define methods, while class attributes are used to define attributes.

Correct answer is: c) Instance attributes have a different value for each instance of a class, while class attributes have the same value for all instances.
Explanation: Instance attributes have unique values for each instance of a class, while class attributes have the same value shared among all instances.

7). Which of the following is true about composition and aggregation in Python?

a) Composition is a stronger form of relationship than aggregation.
b) Aggregation is a stronger form of relationship than composition.
c) Composition implies a “has-a” relationship, while aggregation implies a “part-of” relationship.
d) Composition and aggregation are the same and can be used interchangeably.

Correct answer is: c) Composition implies a “has-a” relationship, while aggregation implies a “part-of” relationship.
Explanation: Composition and aggregation represent different forms of relationships in object-oriented programming. When two objects are composed of one another, there is a clear “has-a” link present. Aggregation implies a looser “part-of” relationship, where one object is associated with other objects.

8). What is the purpose of the `__getitem__` method in Python?

a) It is used to define class attributes.
b) It is used to access items from a class using index-based notation.
c) It is used to compare two instances of a class.
d) It is used to iterate over items in a class.

Correct answer is: b) It is used to access items from a class using index-based notation.
Explanation: The `__getitem__` method in Python is used to define the behavior of accessing items from a class using index-based notation (e.g., `my_object[index]`).

9). What is the purpose of the `__setitem__` method in Python?

a) It is used to define class attributes.
b) It is used to modify items in a class using index-based notation.
c) It is used to convert an instance to a string representation.
d) It is used to compare two instances of a class.

Correct answer is: b) It is used to modify items in a class using index-based notation.
Explanation: The `__setitem__` method in Python is used to define the behavior of modifying items in a class using index-based notation (e.g., `my_object[index] = value`).

10). What is the purpose of the `__len__` method in Python?

a) It is used to define class attributes.
b) It is used to compare two instances of a class.
c) It is used to convert an instance to a string representation.
d) It is used to determine the length of an object.

Correct answer is: d) It is used to determine the length of an object.
Explanation: The `__len__` method in Python is used to define the behavior of determining the length of an object. It is automatically called when the `len()` function is used on an instance of the class.

11). What is the purpose of the `__eq__` method in Python?

a) It is used to define class attributes.
b) It is used to initialize the attributes of a class.
c) It is used to compare two instances of a class for equality.
d) It is used to convert an instance to a string representation.

Correct answer is: c) It is used to compare two instances of a class for equality.
Explanation: The `__eq__` method in Python is used to define the behavior of comparing two instances of a class for equality using the `==` operator.

12). Which of the following is an advantage of using composition over inheritance in Python?

a) Composition provides a more flexible and loosely coupled design.
b) Inheritance allows for code reuse without modifying existing classes.
c) Composition leads to simpler and more concise code.
d) Inheritance provides better performance compared to composition.

Correct answer is: a) Composition provides a more flexible and loosely coupled design.
Explanation: Composition allows for more flexibility and loose coupling between classes, making it easier to change the behavior of a class without modifying its internal structure.

13). What is the purpose of the `__call__` method in Python?

a) It is used to define class attributes.
b) It is used to create a new instance of a class.
c) It is used to invoke an instance of a class as a function.
d) It is used to define a class decorator.

Correct answer is: c) It is used to invoke an instance of a class as a function.
Explanation: The `__call__` method in Python is used to define the behavior of invoking an instance of a class as a function, allowing instances to be called like a function.

14). What is the purpose of the `__iter__` method in Python?

a) It is used to define class attributes.
b) It is used to initialize the attributes of a class.
c) It is used to iterate over items in a class.
d) It is used to compare two instances of a class.

Correct answer is: c) It is used to iterate over items in a class.
Explanation: The `__iter__` method in Python is used to define the behavior of iterating over items in a class using a loop or other iteration constructs.

15). What is the most striking feature of class?

a) Data encapsulation
b) Data abstraction
c) Inheritance
d) Polymorphism

Correct answer is: a) Data encapsulation
Explanation: Data abstraction is the process of hiding the implementation details of an object from the user. This allows the user to focus on the object’s functionality, without having to worry about how it works. Inheritance is the process of one class inheriting the properties and methods of another class. This allows for code reuse and makes it easier to create complex objects. The capacity of an object to assume various forms is known as polymorphism. This is achieved through the use of virtual methods, which can be overridden by subclasses.

16). Why classes are known as abstract data types (ADT)?

a) Because they can be used to represent abstract concepts
b) Because they can be used to represent real-world objects
c) Because they can be used to represent both abstract concepts and real-world objects
d) None of the above

Correct answer is: c) Because they can be used to represent both abstract concepts and real-world objects**
Explanation: Abstract concepts are things that do not have a physical representation in the real world, such as the concept of “love” or the concept of “justice”. Real-world objects are things that have a physical representation in the real world, such as a car or a house. Classes can be used to represent both abstract concepts and real-world objects because they allow the programmer to define the data and the methods that are associated with the object. This allows the programmer to create objects that have the same properties and behavior, regardless of whether they represent an abstract concept or a real-world object.

17). Which of the following is not true about the object-oriented approach?

a) It supports both abstract data and class
b) It provides polymorphism and inheritance
c) It is a programming paradigm
d) It is a way of organizing code

Correct answer is: c) It is a programming paradigm
Explanation: A programming paradigm is a way of organizing code that is based on a particular set of concepts. For example, the procedural programming paradigm is based on the concept of procedures, while the object-oriented programming paradigm is based on the concept of objects. The object-oriented approach is a set of concepts that can be used to implement a programming paradigm. These concepts include classes, objects, methods, inheritance, and polymorphism.

18). Which language among the following support an object-oriented approach?

a) Python
b) C++
c) Java
d) All of the above

Correct answer is: d) All of the above
Explanation: Object-oriented programming is a programming paradigm that uses objects and classes to represent data and behavior. Class instances, or objects, each have a distinct state and behaviour. Classes are templates for creating objects, and they define the data and behavior that all objects of that class will have. Programming languages that focus on objects include Python, C++, and Java. This means that they support the concepts of objects, classes, inheritance, and polymorphism.

19). Which of the following is not a valid attribute of a class?

a) Data members
b) Methods
c) Functions
d) Constructors

Correct answer is: c) Functions
Explanation: Attributes are variables that are associated with a class. They can be used to store data that is common to all objects of that class. Methods are functions that are associated with a class. They can be used to perform operations on objects of that class.

20). Which of the following is not a valid method of a class?

a) init
b) str
c) del
d) main

Correct answer is: d) main
Explanation: The init method is the constructor method of a class. It is called when an object of that class is created. The str method is the string representation method of a class. It is called when an object of that class is converted to a string. The del method is the destructor method of a class. It is called when an object of that class is deleted.

21). What is encapsulation?

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 binding together the data and the functions that operate on that data

Correct answer is: c) The process of hiding the implementation details of an object from the user
Explanation: Encapsulation is one of the most important features of object-oriented programming. It allows for the separation of the interface from the implementation. This makes it simpler to read and maintain the code.

22). What is the difference between an abstract class and an interface?

a) An abstract class is a class that can be instantiated, while an interface is a class that cannot be instantiated
b) An abstract class is a class that can be inherited, while an interface is a class that cannot be inherited
c) An interface can only contain abstract methods, whereas an abstract class can have both abstract and concrete methods.
d) None of the above

Correct answer is: c) An interface can only contain abstract methods, whereas an abstract class can have both abstract and concrete methods.

Leave a Comment