CS FundamentalsVery hardProduct-company placement · Python interview

A Value Object is modelled as a frozen dataclass. What does this print?

A Value Object is modelled as a frozen dataclass. What does this print?
from dataclasses import dataclass, FrozenInstanceError
@dataclass(frozen=True)
class Money:
    amount: int
    currency: str
a = Money(5, "INR"); b = Money(5, "INR")
s = {a, b, Money(5, "USD")}
try:
    a.amount = 6
    r = "changed"
except FrozenInstanceError:
    r = "frozen"
print(a == b, a is b, len(s), r)
  1. A. True True 2 frozen
  2. B. True False 2 frozen
  3. C. False False 3 changed
  4. D. True False 3 frozen

Correct answer: B. True False 2 frozen

Dataclasses compare by field values, so a == b, though they are different objects. frozen=True makes instances immutable and hashable, so a and b collapse to one set element (2 in total), and assignment raises FrozenInstanceError.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic