ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
from dataclasses import dataclass
@dataclass(frozen=True)
class Pt:
    x: int
    y: int = 0
p = Pt(1)
try:
    p.x = 5
except Exception as e:
    print(type(e).__name__, end=" ")
print({p: "ok"}[Pt(1, 0)], Pt(1) < Pt(2) if hasattr(Pt, "__lt__") and Pt.__lt__ is not object.__lt__ else "no order")
  1. A. AttributeError ok True
  2. B. FrozenInstanceError ok True
  3. C. FrozenInstanceError ok no order
  4. D. TypeError ok no order

Correct answer: C. FrozenInstanceError ok no order

frozen=True blocks assignment with dataclasses.FrozenInstanceError and, together with eq=True, generates __hash__, so instances work as dict keys. Ordering methods are generated only with order=True.

More Python Programming questions

Exams that ask this topic