Last Updated: December 6, 2025
28 quizzes
Complete the code to create an immutable data class instance representing a coordinate
point = Coordinate.(x=3, y=4)Click an option to fill the blank:
In a data class, which parameter makes instances immutable after creation?
What is a primary benefit of using __slots__ in a class?
Which built-in is the default metaclass in Python?
A descriptor must implement at least one of which methods?
What does a class decorator receive as its first argument?
What is a typical use case for a mixin class?
When you define __slots__ = ['name'] in a class without __dict__ in slots, what happens?
Which statement about data classes and __slots__ is correct?
What is a key difference between a metaclass and a class decorator?
A data descriptor is one that implements:
Why should most mixin classes avoid defining __init__?
Which is a practical use of a metaclass?
Order the steps to create a data class that validates age using a descriptor.
Drag and drop to reorder, or use the arrows.
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.
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))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)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__'))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)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)Find the bug in this descriptor-based validation code.
Click on the line(s) that contain the bug.
class Positive: def __get__(self, instance, owner): return instance._value def __set__(self, instance, value): if value <= 0: raise ValueError("must be positive") _value = value class Account: balance = Positive() def __init__(self, starting): self.balance = startingFind the bug in this __slots__ and mixin combination.
Click on the line(s) that contain the bug.
class LoggingMixin: def log(self, msg): print(f"LOG: {msg}") class User(LoggingMixin): __slots__ = ('name') def __init__(self, name): super().__init__() self.name = nameMatch 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.
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.
Complete the data class code so that new ids are generated automatically and total is computed.
from dataclasses import dataclass, fieldcurrent_id = 0def next_id() -> int: global current_id current_id += 1 return current_id@dataclassclass Invoice: id: int = field(default_factory=) amount: float @property def total(self) -> float: return Click an option to fill blank 1:
Complete the code to define a mixin that uses a descriptor for validated age.
class AgeField: def __set__(self, instance, value): if value < 0: raise ValueError("age must be non-negative") instance._age = value def __get__(self, instance, owner): return instance._ageclass AgeMixin: age = ()class Person(AgeMixin): def __init__(self, name, age): self.name = name = ageClick an option to fill blank 1:
Click the line that prevents adding new attributes dynamically due to __slots__.
Click on the line to select.
class Record: __slots__ = ('id', 'name') def __init__(self, id, name): self.id = id self.name = name r = Record(1, 'item')# r.extra = 'not allowed' # would raise AttributeErrorClick the line where the metaclass customizes class creation.
Click on the line to select.
class NamingMeta(type): def __new__(mcls, name, bases, attrs): attrs['created_by_meta'] = True return super().__new__(mcls, name, bases, attrs) class Service(metaclass=NamingMeta): pass print(Service.created_by_meta)