QuizzesDataclasses — Boilerplate-Free Data Objects — Concept Check
intermediate
python

Dataclasses — Boilerplate-Free Data Objects — Concept Check

Questions on Dataclasses — Boilerplate-Free Data Objects. @dataclass auto-generates __init__, __repr__ and __eq__ from type-annotated fields — with defaults, immutability (frozen=True), ordering, and field factories for mutables.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Dataclasses — Boilerplate-Free Data Objects — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a data class `Student` with a mutable default for skills. What happens if you forget to use `default_factory`?

    • AIt will work fine without issues.
    • BA ValueError is raised due to mutable default.
    • CThe class won't compile.
    • DThe list will be shared across instances.
  2. 2.A developer uses `frozen=True` in a dataclass. What benefit does this provide?

    • AInstances are only hashable.
    • BInstances are only immutable.
    • CInstances can be sorted automatically.
    • DInstances are immutable and hashable.
  3. 3.Given a dataclass `Version` with `order=True`, what will `sorted([v2, v1])` return if `v1` is Version(1, 9) and `v2` is Version(2, 0)?

    • A[Version(2,0), Version(1,9)]
    • BThe same order as input.
    • CAn error due to unsortable objects.
    • D[Version(1,9), Version(2,0)]
  4. 4.What will happen if you try to modify an attribute of a frozen dataclass instance?

    • AA FrozenInstanceError is raised.
    • BThe program crashes without error.
    • CThe attribute is modified successfully.
    • DIt silently fails without changes.
  5. 5.A dataclass has `order=True` but no `frozen=True`. What is a potential issue?

    • AInstances are hashable but not sortable.
    • BInstances can be sorted but are not hashable.
    • CInstances are neither sortable nor hashable.
    • DInstances are immutable but not hashable.
  6. 6.A developer believes dataclasses are suitable for all classes. What is a flaw in this belief?

    • ADataclasses can't handle any validation.
    • BDataclasses are not compatible with Python 3.7+.
    • CDataclasses are not ideal for behavior-heavy classes.
    • DDataclasses can't have default values.
  7. 7.What is the purpose of `__post_init__` in a dataclass?

    • ATo compare two dataclass instances.
    • BTo convert a dataclass to a dictionary.
    • CTo perform validation after initialization.
    • DTo initialize the class fields.
  8. 8.A dataclass `Version` is defined with `frozen=True` and `order=True`. What can you do with its instances?

    • AOnly sort them.
    • BNeither sort nor use them as dict keys.
    • COnly use them as dict keys.
    • DSort and use them as dict keys.