AlgoMaster Logo

Advanced OOP - Quiz

Last Updated: December 6, 2025

1 min read

Advanced OOP Exercises

28 quizzes

1
Code Completion

Complete the code to create an immutable data class instance representing a coordinate

python
1
point = Coordinate.(x=3, y=4)

Click an option to fill the blank:

2
Multiple Choice

In a data class, which parameter makes instances immutable after creation?

3
Multiple Choice

What is a primary benefit of using __slots__ in a class?

4
Multiple Choice

Which built-in is the default metaclass in Python?

5
Multiple Choice

A descriptor must implement at least one of which methods?

6
Multiple Choice

What does a class decorator receive as its first argument?

7
Multiple Choice

What is a typical use case for a mixin class?

8
Multiple Choice

When you define __slots__ = ['name'] in a class without __dict__ in slots, what happens?

9
Multiple Choice

Which statement about data classes and __slots__ is correct?

10
Multiple Choice

What is a key difference between a metaclass and a class decorator?

11
Multiple Choice

A data descriptor is one that implements:

12
Multiple Choice

Why should most mixin classes avoid defining __init__?

13
Multiple Choice

Which is a practical use of a metaclass?

14
Sequencing

Order the steps to create a data class that validates age using a descriptor.

Drag and drop to reorder, or use the arrows.

15
Sequencing

Order the steps to apply a class decorator that injects a mixin's methods into a target class.

Drag and drop to reorder, or use the arrows.

16
Output Prediction

What is the output of this code using a data class?

1from dataclasses import dataclass
2
3@dataclass
4class Product:
5    name: str
6    price: float
7
8item = Product("Pen", 1.5)
9print(item == Product("Pen", 1.5))
17
Output Prediction

What is the output when accessing a descriptor-backed attribute?

1class Uppercase:
2    def __get__(self, instance, owner):
3        return instance._name.upper()
4    def __set__(self, instance, value):
5        instance._name = value
6
7class User:
8    name = Uppercase()
9
10u = User()
11u.name = "alice"
12print(u.name)
18
Output Prediction

What will this __slots__-based class print?

1class Slotted:
2    __slots__ = ('value',)
3    def __init__(self, value):
4        self.value = value
5
6s = Slotted(10)
7print(hasattr(s, '__dict__'))
19
Output Prediction

What is printed when using a simple class decorator?

1def tagged(cls):
2    cls.tag = "service"
3    return cls
4
5@tagged
6class Service:
7    pass
8
9print(Service.tag)
20
Output Prediction

What is the output of this metaclass-based registration?

1registry = []
2
3class AutoRegister(type):
4    def __new__(mcls, name, bases, attrs):
5        cls = super().__new__(mcls, name, bases, attrs)
6        if name != 'Base':
7            registry.append(name)
8        return cls
9
10class Base(metaclass=AutoRegister):
11    pass
12
13class Task(Base):
14    pass
15
16print(registry)
21
Bug Spotting

Find the bug in this descriptor-based validation code.

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

python
1
class Positive:
2
    def __get__(self, instance, owner):
3
        return instance._value
4
    def __set__(self, instance, value):
5
        if value <= 0:
6
            raise ValueError("must be positive")
7
        _value = value
8
 
9
class Account:
10
    balance = Positive()
11
 
12
    def __init__(self, starting):
13
        self.balance = starting
22
Bug Spotting

Find the bug in this __slots__ and mixin combination.

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

python
1
class LoggingMixin:
2
    def log(self, msg):
3
        print(f"LOG: {msg}")
4
 
5
class User(LoggingMixin):
6
    __slots__ = ('name')
7
 
8
    def __init__(self, name):
9
        super().__init__()
10
        self.name = name
23
Matching

Match each concept with the best description.

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

24
Matching

Match each advanced OOP feature with a realistic use case.

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

25
Fill in the Blanks

Complete the data class code so that new ids are generated automatically and total is computed.

python
1
from dataclasses import dataclass, field
2
3
current_id = 0
4
5
def next_id() -> int:
6
global current_id
7
current_id += 1
8
return current_id
9
10
@dataclass
11
class Invoice:
12
id: int = field(default_factory=)
13
amount: float
14
15
@property
16
def total(self) -> float:
17
return

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to define a mixin that uses a descriptor for validated age.

python
1
class AgeField:
2
def __set__(self, instance, value):
3
if value < 0:
4
raise ValueError("age must be non-negative")
5
instance._age = value
6
def __get__(self, instance, owner):
7
return instance._age
8
9
class AgeMixin:
10
age = ()
11
12
class Person(AgeMixin):
13
def __init__(self, name, age):
14
self.name = name
15
= age

Click an option to fill blank 1:

27
Hotspot Selection

Click the line that prevents adding new attributes dynamically due to __slots__.

Click on the line to select.

python
1
class Record:
2
    __slots__ = ('id', 'name')
3
 
4
    def __init__(self, id, name):
5
        self.id = id
6
        self.name = name
7
 
8
r = Record(1, 'item')
9
# r.extra = 'not allowed'  # would raise AttributeError
28
Hotspot Selection

Click the line where the metaclass customizes class creation.

Click on the line to select.

python
1
class NamingMeta(type):
2
    def __new__(mcls, name, bases, attrs):
3
        attrs['created_by_meta'] = True
4
        return super().__new__(mcls, name, bases, attrs)
5
 
6
class Service(metaclass=NamingMeta):
7
    pass
8
 
9
print(Service.created_by_meta)

Premium Content

Subscribe to unlock full access to this content and more premium articles.