AlgoMaster Logo

Object-Oriented Programming - Quiz

Last Updated: January 3, 2026

Object-Oriented Programming Exercises

31 quizzes

1
Code Completion

Complete the code so that creating a User instance automatically prints its readable description.

python
1
print(User("Ana", 30).())

Click an option to fill the blank:

2
Code Completion

Complete the expression to access the shared tax_rate defined on the Invoice class.

python
1
total_with_tax = subtotal * Invoice.

Click an option to fill the blank:

3
Code Completion

Complete the code to call a class method that creates a Product from a JSON string.

python
1
product = Product.(json_data)

Click an option to fill the blank:

4
Multiple Choice

In OOP, what does encapsulation primarily help you achieve in Python classes?

5
Multiple Choice

What is an object in Python OOP terms?

6
Multiple Choice

Which of the following is TRUE about the __init__ method?

7
Multiple Choice

Which statement about instance attributes is correct?

8
Multiple Choice

What happens if you change a class attribute on the class object itself?

9
Multiple Choice

Which best describes an instance method in Python?

10
Multiple Choice

How is a class method different from an instance method?

11
Multiple Choice

When is using a static method most appropriate?

12
Multiple Choice

Why is self explicitly included as the first parameter of instance methods?

13
Multiple Choice

What does the @property decorator allow you to do?

14
Multiple Choice

Which is a common reason to use a setter with @property?

15
Multiple Choice

Which built-in function calls the __repr__ method on an object?

16
Multiple Choice

What is operator overloading in Python?

17
Sequencing

Order the steps to define a class with __init__ that stores a user's email and then create an instance.

Drag and drop to reorder, or use the arrows.

18
Sequencing

Order the steps to add a @property that exposes a full_name attribute built from first_name and last_name.

Drag and drop to reorder, or use the arrows.

19
Output Prediction

What is the output of this code?

1class Counter:
2    total = 0
3
4    def __init__(self):
5        Counter.total += 1
6
7c1 = Counter()
8c2 = Counter()
9print(Counter.total)
20
Output Prediction

What is the output of this code involving __str__?

1class Task:
2    def __init__(self, title):
3        self.title = title
4
5    def __str__(self):
6        return f"Task: {self.title}"
7
8job = Task("Backup")
9print(job)
21
Output Prediction

What does this code print when using a @property for area?

1class Square:
2    def __init__(self, side):
3        self._side = side
4
5    @property
6    def area(self):
7        return self._side * self._side
8
9s = Square(4)
10print(s.area)
22
Output Prediction

What is the output of this operator overloading example?

1class Score:
2    def __init__(self, value):
3        self.value = value
4
5    def __add__(self, other):
6        return Score(self.value + other.value)
7
8    def __repr__(self):
9        return f"Score({self.value})"
10
11s1 = Score(10)
12s2 = Score(5)
13print(s1 + s2)
23
Output Prediction

What is printed by this class and instance attribute combination?

1class Device:
2    status = "offline"
3
4    def __init__(self, status=None):
5        if status is not None:
6            self.status = status
7
8a = Device()
9b = Device("online")
10print(a.status)
24
Bug Spotting

Find the bug in this code that tries to use a class method as an alternative constructor.

Click on the line(s) that contain the bug.

python
1
class Report:
2
    def __init__(self, title):
3
        self.title = title
4
 
5
    def from_markdown(cls, markdown_text):
6
        title_line = markdown_text.splitlines()[0]
7
        clean_title = title_line.lstrip('# ').strip()
8
        return cls(clean_title)
9
 
10
r = Report.from_markdown("# Sales Report")
25
Bug Spotting

Find the bug related to getters and setters using @property.

Click on the line(s) that contain the bug.

python
1
class Temperature:
2
    def __init__(self, celsius):
3
        self._celsius = celsius
4
 
5
    @property
6
    def celsius(self):
7
        return self._celsius
8
 
9
    @celsius.setter
10
    def celsius(self, value):
11
        if value < -273.15:
12
            raise ValueError("Too cold")
13
        celsius = value
14
 
15
t = Temperature(20)
16
t.celsius = 25
26
Matching

Match each OOP concept to its best description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

27
Matching

Match each dunder method to what it customizes.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Fill in the Blanks

Complete the code so the class uses a @property to expose a read-only full_name built from first and last names.

python
1
class Member:
2
def __init__(self, first_name, last_name):
3
self._first_name = first_name
4
self._last_name = last_name
5
6
7
def full_name(self):
8
return + " " +
9
10
person = Member("Ada", "Lovelace")
11
label = person.full_name

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code so purchases_count is a class-level counter updated each time a new Order is created via the class method.

python
1
class Order:
2
purchases_count = 0
3
4
def __init__(self, product):
5
self.product = product
6
7
8
def from_product(cls, product):
9
.purchases_count += 1
10
return cls(product)
11
12
order1 = Order.from_product("Book")
13
order2 = Order.from_product("Pen")

Click an option to fill blank 1:

30
Hotspot Selection

Click the line where the class attribute discount is being accessed.

Click on the line to select.

python
1
class Invoice:
2
    discount = 0.1
3
 
4
    def __init__(self, total):
5
        self.total = total
6
 
7
bill = Invoice(200)
8
final_amount = bill.total * (1 - Invoice.discount)
9
print(final_amount)
31
Hotspot Selection

Click the line where operator overloading for equality is defined.

Click on the line to select.

python
1
class Point:
2
    def __init__(self, x, y):
3
        self.x = x
4
        self.y = y
5
 
6
    def __eq__(self, other):
7
        return self.x == other.x and self.y == other.y
8
 
9
p1 = Point(1, 2)
10
p2 = Point(1, 2)
11
print(p1 == p2)