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)- A. True True 2 frozen
- B. True False 2 frozen
- C. False False 3 changed
- 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.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- This Singleton caches its instance in new. What does it print? python class Settings: instance = None def new(cls, args): if cls.instance…
- Module report.py does from pricing import getrate and calls getrate() inside build(). A unit test for build() uses…
- A faulty merge M of feature into main was undone with git revert -m 1 M. The feature team later fixes the bug with a new commit on feature…
- You run git stash pop and it reports a merge conflict in f.txt. What happens to the stash entry?
- During git rebase -i, what is the difference between marking a commit 'squash' and marking it 'fixup'?
- On main you need the version of config.yml that exists on branch release, without merging anything else. Which command does this, and what…