Answer key · 12 questions

Python Dataclasses Exercises With Answers

All 12 questions for Python Dataclasses in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Dataclasses, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

Which decorator turns a class into a dataclass?

  1. @property
  2. @dataclasscorrect
  3. @staticmethod
  4. @classmethod

Answer: @dataclass

Why: The dataclass decorator reads the annotated fields and generates common methods.

02 · Multiple choice

What does a dataclass generate by default?

  1. A database table
  2. __init__, __repr__, and __eq__correct
  3. A JSON file
  4. Only __init__

Answer: __init__, __repr__, and __eq__

Why: The default dataclass provides initialization, display, and value equality.

03 · Multiple choice

What does frozen=True prevent?

  1. Attribute assignmentcorrect
  2. Creating instances
  3. Calling asdict
  4. Reading fields

Answer: Attribute assignment

Why: A frozen dataclass raises an error when an attribute is assigned after creation.

04 · Multiple choice

What does the program output?

from dataclasses import dataclass
@dataclass
class P:
    x: int
print(P(1))
  1. 1
  2. P(1)
  3. {'x': 1}
  4. P(x=1)correct

Answer: P(x=1)

Why: The generated repr includes the class name and field values.

05 · Fill in the blank

Complete the module that provides dataclass.

from ____ import dataclass

Answer: dataclasses

Why: The decorator is imported from dataclasses.

06 · Fill in the blank

Complete the field annotation.

from dataclasses import dataclass
@dataclass
class P:
    x: ____

Answer: int

Why: x is declared as an integer field.

07 · Fill in the blank

Complete the default value for active.

from dataclasses import dataclass
@dataclass
class U:
    name: str
    active: bool = ____

Answer: True

Why: active defaults to True when it is omitted.

08 · Fill in the blank

Complete the function that converts the instance to a dictionary.

from dataclasses import dataclass, asdict
@dataclass
class P:
    x: int
p = P(2)
print(____(p))

Answer: asdict

Why: asdict returns a plain dictionary of field names and values.

09 · Predict output

What appears on the screen?

from dataclasses import dataclass
@dataclass
class P:
    x: int
print(P(2))

Answer: P(x=2)

Why: The generated repr shows the class and the field, P(x=2).

10 · Predict output

What is printed?

from dataclasses import dataclass
@dataclass
class P:
    x: int
print(P(1) == P(1))

Answer: True

Why: Dataclasses compare their fields, so two instances with x=1 are equal.

11 · Predict output

What is the output?

from dataclasses import dataclass
@dataclass
class U:
    name: str
    active: bool = True
print(U("Ada").active)

Answer: True

Why: The default active value is True, and the instance exposes it.

12 · Predict output

What does this code print?

from dataclasses import dataclass, asdict
@dataclass
class P:
    x: int
print(asdict(P(2)))

Answer: {'x': 2}

Why: asdict returns a plain dictionary with the field value.

Try the questions first

Open the practice page for Python Dataclasses and answer the same 12 questions with instant feedback.

Practice this lesson