Python Object-Oriented Programming Basics 4 — Questions and Answers
Question 1: What is a mixin in Python OOP?
- A class that holds only class-level constants
- A small class designed to add specific behavior via multiple inheritance (Correct answer)
- A built-in decorator for combining classes
- A subclass that overrides every parent method
Correct answer: A small class designed to add specific behavior via multiple inheritance
Mixins are narrowly focused classes added to a class hierarchy via multiple inheritance to inject reusable behavior without deep coupling.
Question 2: What does `__slots__` do when defined in a class?
- Limits the number of methods
- Replaces `__dict__` to restrict instance attributes to a fixed set (Correct answer)
- Enforces type checking on attributes
- Converts instance attributes to class attributes
Correct answer: Replaces `__dict__` to restrict instance attributes to a fixed set
Declaring `__slots__` removes the per-instance `__dict__`, reducing memory and preventing dynamic attribute creation.
Question 3: Which of the following is an example of polymorphism in Python?
- Two classes both define a `speak()` method with different behaviors (Correct answer)
- A class inheriting from two parent classes
- Using `@staticmethod` to avoid instance variables
- Defining `__init__` in every class
Correct answer: Two classes both define a `speak()` method with different behaviors
Polymorphism allows different classes to implement the same interface (e.g., `speak()`), enabling generic code to work with multiple types.
Question 4: What is the difference between `@classmethod` and `@staticmethod`?
- Class methods receive `cls`; static methods receive neither `self` nor `cls` (Correct answer)
- Static methods receive `cls`; class methods receive `self`
- They are identical in behavior
- Class methods can only be called on instances
Correct answer: Class methods receive `cls`; static methods receive neither `self` nor `cls`
`@classmethod` passes the class as its first argument (`cls`), while `@staticmethod` is a plain function scoped to the class.
Question 5: In Python, what does `__del__` define?
- A method to delete a class attribute
- A destructor called when the object is about to be garbage collected (Correct answer)
- A method to remove an item from a list attribute
- The logic for the `del` keyword on the class itself
Correct answer: A destructor called when the object is about to be garbage collected
`__del__` is the finalizer, invoked just before the interpreter destroys the object, though its exact timing is not guaranteed.
Question 6: Which of the following correctly uses `__eq__` to compare two objects?
- def __eq__(self, other): return self.value == other
- def __eq__(self, other): return self.value == other.value (Correct answer)
- def __eq__(cls, other): return cls.value == other.value
- def __eq__(self): return self.value
Correct answer: def __eq__(self, other): return self.value == other.value
`__eq__` should compare the relevant attribute of `self` with the same attribute of `other`, returning a boolean.
Question 7: What is encapsulation in OOP?
- Inheriting behavior from a parent class
- Bundling data and methods that operate on that data within a class, restricting direct access (Correct answer)
- Defining a class inside another class
- Using decorators to wrap methods
Correct answer: Bundling data and methods that operate on that data within a class, restricting direct access
Encapsulation hides internal state and exposes only what's necessary through a public interface, protecting the object's integrity.
What is a mixin in Python OOP?