Python Object-Oriented Programming Basics 3 — Questions and Answers
Question 1: What is the purpose of a `@staticmethod` in Python?
- It can access the class but not the instance
- It can access the instance but not the class
- It cannot access the class or instance (Correct answer)
- It automatically calls `__init__`
Correct answer: It cannot access the class or instance
Static methods belong to the class namespace but receive neither `self` nor `cls`, so they cannot modify class or instance state.
Question 2: What does `isinstance(obj, MyClass)` return?
- True only if obj's type is exactly MyClass
- True if obj is an instance of MyClass or any of its subclasses (Correct answer)
- The class of obj
- False if obj has extra attributes
Correct answer: True if obj is an instance of MyClass or any of its subclasses
`isinstance()` returns `True` for the specified class and all its subclasses, supporting polymorphic checks.
Question 3: In Python, what is the Method Resolution Order (MRO)?
- The order in which methods are alphabetically sorted
- The order Python searches base classes when resolving a method call (Correct answer)
- The order in which methods are defined in a class
- The sequence of `__init__` calls for all superclasses
Correct answer: The order Python searches base classes when resolving a method call
MRO, computed by the C3 linearization algorithm, determines which class's method is invoked in multiple-inheritance hierarchies.
Question 4: What does the `@property` decorator do?
- Turns a method into a class variable
- Allows a method to be accessed like an attribute (Correct answer)
- Prevents the attribute from being set
- Makes the attribute public
Correct answer: Allows a method to be accessed like an attribute
`@property` lets you define getter logic for an attribute so callers use `obj.name` instead of `obj.get_name()`.
Question 5: Which of the following demonstrates composition over inheritance?
- class Dog(Animal): pass
- class Car: def __init__(self): self.engine = Engine() (Correct answer)
- class A(B, C): pass
- super().__init__()
Correct answer: class Car: def __init__(self): self.engine = Engine()
Composition means a class contains an instance of another class as an attribute, favoring 'has-a' over 'is-a' relationships.
Question 6: What happens when you call `del obj.attr` where `attr` has a `@property` setter but no deleter?
- The attribute is silently removed
- Python raises an AttributeError (Correct answer)
- The property is converted to a plain attribute
- Python calls the setter with None
Correct answer: Python raises an AttributeError
Without a `@attr.deleter` defined, attempting to delete the property raises `AttributeError: can't delete attribute`.
Question 7: Which dunder method allows instances to be used as callables (e.g., `obj()`)?
- __run__
- __execute__
- __call__ (Correct answer)
- __invoke__
Correct answer: __call__
Defining `__call__` on a class makes its instances callable, executing the `__call__` body when `obj()` is used.
What is the purpose of a `@staticmethod` in Python?