Polymorphism Activity
Polymorphism Activity
CPE201
I. Multiple choice
1. Which of the following best describes abstraction in object-oriented programming?
a. Encapsulating data within a class
b. Hiding the implementation details and showing only the functionality
c. Inheriting properties from a parent class
d. Providing multiple implementations of a function
2. Polymorphism allows objects to be treated as instances of their parent class. Which
type of polymorphism does this statement refer to?
a. Compile-time polymorphism
b. Run-time polymorphism
c. Overloading
d. Overriding
3. Which of the following is NOT true about abstract classes in Python?
a. Abstract classes can be instantiated directly.
b. Abstract classes can contain abstract methods.
c. Abstract classes can provide default implementations of some methods.
d. Subclasses of abstract classes must implement all abstract methods.
4. In Python, which module provides support for defining abstract base classes?
a. collections
b. abc
c. abstract
d. base
5. Which of the following statements best describes abstraction in object-oriented
programming?
a. Abstraction involves combining data and methods that operate on the data into a
single unit.
b. Abstraction means using the same function name for different purposes.
c. Abstraction is the process of hiding the implementation details and showing only
the functionality to the user.
d. Abstraction allows objects to be treated as instances of their parent class.
II.
2. Describe polymorphism and how it can be achieved in Python. Provide an example using a
class hierarchy.
- Polymorphism is like having a single name with multiple forms. In Python, we can achieve
polymorphism through method overriding in inheritance. Let's say we have a class hierarchy
with a base class called Animal and two subclasses called Dog and Cat. Each subclass can have
its own implementation of a method, let's say make_sound(). When we call make_sound() on an
object of type Animal, it behaves differently based on whether the object is a Dog or a Cat,
exhibiting polymorphic behavior.
3. What are the main differences between abstraction and encapsulation? Provide examples to
illustrate your points.
- Abstraction and encapsulation are both ways of managing complexity in software, but they
serve different purposes. Abstraction focuses on hiding the implementation details and showing
only the essential features, like driving a car without knowing how the engine works.
Encapsulation, on the other hand, involves bundling the data and methods that operate on the
data into a single unit, like putting all the car's controls and mechanisms inside its body. So,
while abstraction shields us from complexity by showing only what's necessary, encapsulation
protects the inner workings of an object, preventing direct access and manipulation from outside.
III.