Object-Oriented Programming with Python | Worksheet
Object-Oriented Programming
with Python
Worksheet · 25 Questions
Topics: Classes · Inheritance · Encapsulation · Polymorphism · Abstraction
Section 1: Theory & Concepts
Q1. [Theory]
What are the four pillars of Object-Oriented Programming? Briefly describe each one.
Object-Oriented Programming (OOP) is built on four core principles designed to
make code more modular, flexible, and maintainable.
Q2. [MCQ]
Which keyword is used to create a class in Python?
A) def
B) object
C) class
D) struct
Q3. [Theory]
Explain the difference between a class and an object (instance). Give a real-world analogy to
support your answer.
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q4. [MCQ]
What is the purpose of the __init__ method in a Python class?
A) It destroys the object when it is no longer needed
B) It initialises the object's attributes when a new instance is created
C) It defines the class name
D) It is called every time a method is invoked
Q5. [Theory]
What is encapsulation? How does Python implement it using naming conventions? Explain the
difference between public, protected, and private attributes.
Q6. [Theory]
Define inheritance in OOP. What are the benefits of using inheritance in a software project?
Q7. [MCQ]
Which built-in function returns True if an object is an instance of a given class or its subclass?
A) type()
B) issubclass()
C) isinstance()
D) hasattr()
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q8. [Theory]
Explain polymorphism with an example. How does Python support polymorphism without strict type
declarations?
Q9. [Theory]
What is the difference between method overriding and method overloading? Does Python support
both? Explain.
Q10. [Theory]
What is abstraction in OOP? How can you create an abstract class in Python? Which module do
you need to import?
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Section 2: Code Reading & Analysis
Q11. [Theory]
Study the class below and answer the questions that follow.
class Animal:
def __init__(self, name, sound):
[Link] = name
self.__sound = sound
def speak(self):
return f'{[Link]} says {self.__sound}'
def get_sound(self):
return self.__sound
dog = Animal('Rex', 'Woof')
print([Link]())
print(dog.__sound) # Line A
a) What will print([Link]()) output?
b) What will happen on Line A and why?
Q12. [Theory]
Read the code below. What is printed to the console, and in what order? Explain why.
class Shape:
def area(self):
return 0
class Circle(Shape):
def __init__(self, radius):
[Link] = radius
def area(self):
return 3.14159 * [Link] ** 2
class Square(Shape):
def __init__(self, side):
[Link] = side
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
def area(self):
return [Link] ** 2
shapes = [Circle(5), Square(4), Shape()]
for s in shapes:
print([Link]())
Q13. [Theory]
What does the super() function do? Trace through the code below and write the exact output.
class Vehicle:
def __init__(self, make):
[Link] = make
print(f'Vehicle created: {[Link]}')
class Car(Vehicle):
def __init__(self, make, model):
super().__init__(make)
[Link] = model
print(f'Car created: {[Link]} {[Link]}')
c = Car('Toyota', 'Corolla')
Q14. [MCQ]
What are dunder (magic) methods in Python? Which of the following is NOT a dunder method?
A) __str__
B) __len__
C) __repr__
D) __display__
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q15. [Theory]
What is the difference between a class method (@classmethod), a static method (@staticmethod),
and an instance method? Give a use case for each.
Section 3: Coding Exercises
Q16. [Coding]
Write a Python class called Rectangle with the following requirements:
• Attributes: width and height (passed to __init__)
• Method area() that returns the area
• Method perimeter() that returns the perimeter
• A __str__ method that returns a readable description
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q17. [Coding]
Create a base class called Person with attributes name and age and a method introduce() that
prints a greeting. Then create a subclass called Student that inherits from Person and adds an
attribute student_id and overrides introduce() to include the student ID in the greeting.
Q18. [Coding]
Write a Python class BankAccount that demonstrates encapsulation:
• The balance should be a private attribute (__balance)
• Include deposit(amount) and withdraw(amount) methods with validation
• A get_balance() method to safely read the balance
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q19. [Coding]
Using Python's abc module, create an abstract class called Shape with an abstract method area().
Then create two concrete subclasses: Triangle and Circle, each implementing area(). Finally, write
code that creates one instance of each and prints their areas.
Q20. [Coding]
Write a class called Temperature that:
• Stores a temperature value in Celsius
• Has a property celsius that uses a getter and setter (use @property and @[Link])
• The setter should raise a ValueError if the temperature is below -273.15°C
• Has a method to_fahrenheit() that returns the equivalent Fahrenheit value
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Section 4: Debugging & Problem Solving
Q21. [Debug]
The following code contains errors. Identify all the bugs and write the corrected version below.
class Dog
def _init_(self, name, breed):
name = name
[Link] = breed
def bark(self):
print(f{[Link]} says Woof!')
my_dog = Dog('Buddy', 'Labrador')
my_dog.bark()
Bugs found:
Corrected code:
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q22. [Debug]
The programmer intended to create a counter that tracks the total number of instances created
using a class variable. Find and fix the bug.
class Counter:
count = 0
def __init__(self):
count += 1
def get_count(self):
return [Link]
a = Counter()
b = Counter()
print(a.get_count())
Explain the bug:
Fixed code:
Q23. [Coding]
Implement the __add__ dunder method for a Vector class so that two vectors can be added
together using the + operator. The class should also implement __str__ to display the vector neatly.
Demonstrate its use with two vectors.
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Q24. [Theory]
What is multiple inheritance in Python? Write a short example demonstrating it, and explain what
the Method Resolution Order (MRO) is and how Python determines it. What problem can arise with
multiple inheritance?
Q25. [Coding]
Design a mini library system using OOP. Create at least two classes (e.g., Library and Book). Your
solution should demonstrate: at least one form of inheritance or composition, encapsulation of at
least one attribute, and a meaningful __str__ method. Include a short driver script showing your
classes in use.
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
— End of Worksheet —
Section 1: Theory & Concepts
Q1. What are the four pillars of Object-Oriented Programming? Briefly describe each one.
Encapsulation: Grouping data and methods into a single unit (class) and hiding internal
states to protect data.
Abstraction: Hiding complex implementation details and showing only necessary features
to the user.
Inheritance: Allowing a new class to derive attributes and methods from an existing class to
reuse code.
Polymorphism: Allowing different classes to be treated as instances of a common parent
class through the same interface.
Q2. Which keyword is used to create a class in Python?
Answer: C) class
Q3. Explain the difference between a class and an object (instance). Give a real-world
analogy.
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Difference: A class is a blueprint or template for creating objects, while an object is a
specific instance created from that blueprint.
Analogy: A class is like a blueprint for a house; an object is the actual house built on the
street.
Q4. What is the purpose of the __init__ method in a Python class?
Answer: B) It initialises the object's attributes when a new instance is created.
Q5. What is encapsulation? How does Python implement it using naming conventions?
Encapsulation: Bundling data and methods while restricting access to some components.
Public: Attributes accessible from anywhere (e.g., [Link]).
Protected: Indicated by a single underscore (e.g., self._price); should only be accessed
within the class or subclasses.
Private: Indicated by double underscores (e.g., self.__balance); triggers name mangling to
prevent outside access.
Q6. Define inheritance in OOP. What are the benefits?
Definition: A mechanism where a child class inherits properties from a parent class.
Benefits: Promotes code reusability, simplifies maintenance, and allows for hierarchical
organization.
Q7. Which built-in function returns True if an object is an instance of a given class?
Answer: C) isinstance()
Q8. Explain polymorphism with an example. How does Python support it?
Explanation: Different classes can define methods with the same name. For example, both
Dog and Cat classes could have a speak() method that produces different sounds.
Python Support: Python uses Duck Typing, where the type or class of an object is less
important than the methods it defines.
Q9. Method overriding vs. overloading. Does Python support both?
Overriding: Redefining a parent class method in a child class. Supported in Python.
Overloading: Having multiple methods with the same name but different parameters.
Python does not support traditional overloading; the last defined method replaces previous
ones.
Q10. What is abstraction? How do you create an abstract class in Python?
Abstraction: Focusing on "what" an object does rather than "how".
Implementation: Use the abc module, inherit from ABC, and use the @abstractmethod
decorator.
Section 2: Code Reading & Analysis
Q11. Study the Animal class.
a) Output: Rex says Woof
b) Line A: This will cause an AttributeError because __sound is a private attribute and
cannot be accessed directly outside the class.
Q12. What is printed and in what order?
Output: 1. 78.53975 (Circle area: $3.14159 \times 5^2$) 2. 16 (Square area: $4 \times 4$)
3. 0 (Shape default area)
Explanation: Python uses polymorphism to call the specific area() method for each object in
the list.
Q13. What does super() do? Exact output?
super(): Calls methods from the parent class (usually the constructor).
Output:
Vehicle created: Toyota
Car created: Toyota Corolla
Q14. Which is NOT a dunder method?
Answer: D) display
Q15. Differences between methods:
Instance Method: Takes self; operates on specific instance data.
Class Method: Takes cls and the @classmethod decorator; operates on class-level data.
Name: ________________________________ Date: ______________ Score: _______ / 25
Object-Oriented Programming with Python | Worksheet
Static Method: Uses @staticmethod; doesn't take self or cls. Used for utility functions.
Section 3: Coding Exercises
Q16. Rectangle Class
Python
class Rectangle:
def __init__(self, width, height):
[Link] = width
[Link] = height
def area(self):
return [Link] * [Link]
def perimeter(self):
return 2 * ([Link] + [Link])
def __str__(self):
return f"Rectangle(Width: {[Link]}, Height: {[Link]})"
Name: ________________________________ Date: ______________ Score: _______ / 25