CS FundamentalsVery hardGATE CS · Software Engineering · Advanced
What is printed?
What is printed?
class Cfg:
_inst = None
def __new__(cls, *args):
if cls._inst is None:
cls._inst = super().__new__(cls)
return cls._inst
def __init__(self, v):
self.v = v
a = Cfg(1)
b = Cfg(2)
print(a is b, a.v, b.v)- A. True 1 1
- B. True 2 2
- C. False 1 2
- D. True 1 2
Correct answer: B. True 2 2
__new__ returns the same instance both times, but Python still calls __init__ on it for every Cfg(...) call, so the second call overwrites v with 2 for both names. A singleton that must keep its first state has to guard __init__ as well.
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
- A Kanban team's board holds on average 15 items (queued or in progress), and over a long period the team delivers 20 items in every 10…
- A method contains 3 if statements, 1 while loop and 1 for loop, with no && or || operators and no other branching. What is its cyclomatic…
- After committing by mistake, a developer runs git reset --soft HEAD~1. What is the result?
- A field accepts ages from 18 to 60 inclusive. Which set of test values best applies boundary value analysis?
- In Scrum, who is accountable for ordering the items in the Product Backlog?
- The question "Are we building the right product?" is answered by which activity?