CS FundamentalsVery hardProduct-company placement · Python
In this Observer code, handler a unsubscribes itself the first time it runs. What does it print (lines joined by ' / ')?
In this Observer code, handler a unsubscribes itself the first time it runs. What does it print (lines joined by ' / ')?
class Bus:
def __init__(self): self.subs = []
def emit(self, e):
for h in self.subs: h(e)
bus = Bus()
def a(e): print('a:' + e); bus.subs.remove(a)
def b(e): print('b:' + e)
def c(e): print('c:' + e)
bus.subs = [a, b, c]
bus.emit('x'); bus.emit('y')- A. a:x / c:x / b:y / c:y
- B. a:x / b:x / c:x / b:y / c:y
- C. a:x / b:x / c:x / a:y / b:y / c:y
- D. a:x / c:x / c:y
Correct answer: A. a:x / c:x / b:y / c:y
Removing a shifts b into index 0 while the loop's index moves to 1, so b is skipped during the first emit. The second emit sees [b, c]. Subjects should iterate over a copy (for h in list(self.subs)).
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 class gets its dependencies by calling ServiceLocator.get('Mailer') inside its methods, instead of receiving them through its…
- In a layered application, which pairing of the Repository and Unit of Work patterns is correct?
- How many of these statements about event sourcing are correct? 1. The append-only log of domain events is the source of truth, and current…
- Match each anti-pattern with its description. P. Golden hammer Q. Lava flow R. God object S. Cargo cult 1. One class knows and does far…
- A release burndown has stayed almost flat for three sprints, yet the team's velocity has been a steady 30 points per sprint. Which chart…
- A work item spent 4 days waiting for development, 2 days in development, 5 days waiting for review, 0.5 day in review, 3 days waiting for…