CS FundamentalsVery hardGATE CS · Software Engineering · Advanced
What is printed?
What is printed?
class S:
def __init__(self): self.obs = []
def notify(self, x):
for o in self.obs:
o(x)
log = []
def a(x):
log.append(('a', x)); s.obs.remove(a)
def b(x):
log.append(('b', x))
s = S()
s.obs += [a, b]
s.notify(1)
s.notify(2)
print(log)- A. [('a', 1), ('b', 1), ('a', 2), ('b', 2)]
- B. [('a', 1), ('b', 2)]
- C. [('a', 1), ('b', 1), ('b', 2)]
- D. [('a', 1), ('a', 2), ('b', 2)]
Correct answer: B. [('a', 1), ('b', 2)]
Removing a from the list while the for loop iterates over it shifts b into the slot the iterator has already passed, so b is skipped in the first notification; in the second notification only b remains. A copy (list(self.obs)) would avoid the bug.
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
- What is printed? python class Cfg: inst = None def new(cls, args): if cls.inst is None: cls.inst = super().new(cls) return cls.inst def…
- 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?