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")- A. AttributeError ok True
- B. FrozenInstanceError ok True
- C. FrozenInstanceError ok no order
- 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
- What is printed? python nums = [1, 2, 3, 4, 5, 6] evens = list(filter(lambda n: n % 2 == 0, nums)) total = sum(n n for n in nums if n % 2)…
- What is printed? python def chunks(xs, k): for i in range(0, len(xs), k): yield xs[i:i + k] def flatten(seq): for s in seq: if…
- What is printed? (Output lines are shown separated by " / ".) python def f(a, b, /, c, , d): return (a, b, c, d) args = (1, 2) kw = {"c"…
- What is printed? python class A: def init(self, kw): print("A", sorted(kw), end=" ") class B(A): def init(self, b=0, kw): print("B", end="…
- What is printed? python class Base: def run(self): return self.step() def step(self): return "base" class Child(Base): def step(self)…
- What is printed? python class Meta(type): def new(m, name, bases, ns): ns["kind"] = name.upper() return super().new(m, name, bases, ns)…