CS FundamentalsHardProduct-company placement
What does this Chain of Responsibility code print?
What does this Chain of Responsibility code print?
class Handler:
def __init__(self, limit, name, nxt=None):
self.limit, self.name, self.nxt = limit, name, nxt
def handle(self, amt):
if amt <= self.limit:
return self.name
if self.nxt:
return self.nxt.handle(amt)
return "rejected"
chain = Handler(1000, "clerk", Handler(10000, "manager", Handler(50000, "director")))
print([chain.handle(x) for x in (1000, 1001, 50000, 50001)])- A. ['clerk', 'manager', 'director', 'rejected']
- B. ['manager', 'manager', 'director', 'director']
- C. ['clerk', 'manager', 'rejected', 'rejected']
- D. ['clerk', 'clerk', 'director', 'rejected']
Correct answer: A. ['clerk', 'manager', 'director', 'rejected']
Each handler takes amounts up to and including its limit and otherwise passes the request on. 1000 stops at the clerk, 1001 at the manager, 50000 at the director, and 50001 falls off the end of the chain.
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 does this Singleton implemented with a metaclass print? python class Meta(type): inst = {} def call(cls, a, k): if cls not in…
- This Observer code registers three listeners. What does it print? python class Subject: def init(self): self.obs = [] def attach(self, f)…
- A drawing app has 4 shape types and must render each through 3 back-ends (SVG, Canvas, PDF). Building one subclass per combination needs…
- A compiler has a stable set of AST node classes (Number, BinaryOp, Call, Variable) but keeps adding new operations: type-check…
- A file-manager must compute the total size of a folder, where a folder may contain files and other folders, and client code should call…
- A text editor needs multi-level undo and redo, and also wants to queue and log user actions such as 'insert text' or 'delete line'. Which…