Welcome to MCQ (Multiple Choice Questions) on Object-Oriented Programming (OOP) in Python! Whether you are a newcomer taking your first steps in Python or someone looking to brush up your OOP concepts, this set of 20 Multiple Choice Questions (MCQs) will serve as a great learning resource.
OOP is one of the foundational paradigms in programming and is extensively used in Python to organize code into reusable structures. It not only enhances code readability but also makes it easier to manage and maintain. Understanding the basic concepts like classes, objects, inheritance, and polymorphism is critical for any Python programmer.
This MCQ covers key aspects of OOP in Python, ranging from defining classes and creating objects to more advanced topics like method overriding and encapsulation. Each question comes with a detailed explanation to help you grasp the underlying principles. So, are you ready to test your understanding of OOP in Python? Let’s dive in!
1. Which of the following is not a key concept in OOP?
Answer:
Explanation:
While inheritance, polymorphism, and encapsulation are core OOP concepts, multiprocessing pertains to concurrent execution and isn’t specific to OOP.
2. In Python, an object is:
Answer:
Explanation:
In OOP, an object is an encapsulation of data (attributes) and methods (functions) that operate on the data.
3. Which of the following is correct about class attributes?
Answer:
Explanation:
Class attributes, unlike instance attributes, are shared across all instances of the class.
4. What is the purpose of a constructor in a class?
Answer:
Explanation:
Constructors are special methods that get invoked when an object is instantiated, primarily used to initialize attributes.
5. In Python, how is the constructor defined for a class?
Answer:
Explanation:
In Python, the constructor method is named __init__().
6. What is the concept of inheriting properties of one class into another class?
Answer:
Explanation:
Inheritance allows one class (child/subclass) to inherit attributes and methods from another class (parent/superclass).
7. Which of the following keywords is used to create a subclass in Python?
Answer:
Explanation:
The class keyword is used, but the inheritance relationship is determined by the parentheses. For example: class SubClass(ParentClass):.
8. What does method overriding mean in Python?
Answer:
Explanation:
Method overriding involves defining in the subclass a method with the same name as in the superclass, thus altering its behavior in the derived class.
9. How does Python support method overloading?
Answer:
Explanation:
Unlike some other languages where method overloading is achieved by having multiple methods with the same name but different parameters, Python achieves this using default arguments and variable-length argument lists (using *args and **kwargs).
10. In method overriding, if the subclass has the same method as declared in the parent class, which version of the method will be executed?
Answer:
Explanation:
If a method is overridden in the subclass, the version of the method defined in the subclass will be executed, not the one in the parent class.
11. What’s the primary use of the super() function in Python OOP?
Answer:
Explanation:
super() allows us to call a method from the parent class, commonly used in the constructor (__init__) of a subclass to initialize attributes from the superclass.
12. Which of the following is a special method in Python?
Answer:
Explanation:
In Python, methods wrapped with double underscores (e.g., __init__) are special methods, often called “magic” or “dunder” methods.
13. How do you declare a private attribute in a class?
Answer:
Explanation:
In Python, attributes prefixed with double underscores are considered private and can’t be accessed directly outside the class (though there are ways around this due to Python’s nature of name mangling).
14. What is the concept of using one operation to work with different types of objects?
Answer:
Explanation:
Polymorphism, from Greek words meaning “many shapes”, allows one interface to be used for a general class of actions, irrespective of the types of objects.
15. Which of the following can be used to restrict access to members of a class?
Answer:
Explanation:
Prefixing an attribute with a single underscore (e.g., _attr) conventionally indicates it is protected and shouldn’t be accessed outside the class, though it still can be. It’s more of a convention than an enforced restriction.
16. Which method gets called when an object is deleted?
Answer:
Explanation:
The __del__ method is a destructor and gets called when an object is deleted.
17. What does the isinstance() function do?
Answer:
Explanation:
The isinstance() function returns True if the object is an instance of the class or a tuple of classes.
18. Which of the following is not a type of class method?
Answer:
Explanation:
Python classes have instance methods, static methods, and class methods. There’s no “modular method.”
19. What is the primary difference between a class method and a static method?
Answer:
Explanation:
A class method takes a reference to the class (typically cls) as its first parameter, allowing it to access and modify class-level attributes. In contrast, static methods don’t have access to class or instance-specific data and behave like plain functions.
20. In a class definition, what does a property decorator (@property) do?
Answer:
Explanation:
The @property decorator allows us to define methods in a class that are accessible like attributes without needing to invoke them like functions.