POC Object-Oriented Programming in Python 3 — Questions and Answers
Question 1: What is polymorphism in the context of Python OOP?
- The ability to create multiple instances of the same class
- The ability of different objects to respond to the same method call in different ways (Correct answer)
- Using multiple inheritance to combine classes
- Restricting access to class attributes
Correct answer: The ability of different objects to respond to the same method call in different ways
Polymorphism allows objects of different types to be treated through a common interface, each responding according to its own implementation.
Question 2: Which of the following is an example of duck typing in Python?
- Checking an object's type with `type()` before calling a method
- Calling a method on an object regardless of its class, trusting it has that method (Correct answer)
- Using `isinstance()` to verify class membership before use
- Inheriting from a common abstract base class
Correct answer: Calling a method on an object regardless of its class, trusting it has that method
Duck typing means Python calls a method on any object that has it, without checking the object's actual type first.
Question 3: What is the output of calling `type(42)`?
- <class 'number'>
- <class 'int'> (Correct answer)
- <class 'integer'>
- int
Correct answer: <class 'int'>
`type(42)` returns `<class 'int'>` because 42 is an integer object in Python.
Question 4: What does the `@staticmethod` decorator do?
- Binds a method to the class rather than an instance, receiving `cls` as the first argument
- Defines a method that does not receive the instance or class as an implicit first argument (Correct answer)
- Makes a method immutable
- Automatically caches method results
Correct answer: Defines a method that does not receive the instance or class as an implicit first argument
A static method doesn't receive `self` or `cls`; it behaves like a regular function scoped inside the class.
Question 5: Which method is called when an object is about to be garbage collected?
- __init__
- __delete__
- __del__ (Correct answer)
- __exit__
Correct answer: __del__
`__del__` is the destructor method, called when the object's reference count drops to zero.
Question 6: What is an abstract class in Python?
- A class with no methods defined
- A class that cannot be instantiated and is meant to be subclassed, often using the `abc` module (Correct answer)
- A class that only contains static methods
- A class defined inside another class
Correct answer: A class that cannot be instantiated and is meant to be subclassed, often using the `abc` module
Abstract classes (via `abc.ABC` and `@abstractmethod`) enforce that subclasses implement specific methods before instantiation.
Question 7: Given `class B(A):`, what happens if `B` does not override method `foo` defined in `A`?
- Calling `b.foo()` raises an AttributeError
- Calling `b.foo()` executes `A`'s version of `foo` (Correct answer)
- Python automatically creates an empty `foo` in `B`
- The method is deleted from `B`
Correct answer: Calling `b.foo()` executes `A`'s version of `foo`
Python's MRO (Method Resolution Order) looks up the inheritance chain, so `B` inherits and can use `A`'s `foo` unchanged.
What is polymorphism in the context of Python OOP?