CS FundamentalsHardProduct-company placement · Python
What happens when this builder code runs?
What happens when this builder code runs?
class QB:
def __init__(self): self.parts = []
def where(self, c): self.parts.append(c); return self
def limit(self, n): self.parts.append(f'LIMIT {n}')
q = QB().where('a>1').limit(5).where('b<2')
print(q.parts)- A. It prints ['a>1', 'LIMIT 5', 'b<2'] in call order
- B. It prints ['a>1', 'b<2', 'LIMIT 5'], clauses first
- C. AttributeError, since limit returns None
- D. It prints ['a>1', 'LIMIT 5'] and skips the last where
Correct answer: C. AttributeError, since limit returns None
Fluent interfaces need every chained method to return self. limit has no return statement, so it returns None and None.where(...) raises AttributeError: 'NoneType' object has no attribute 'where'.
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 plugin-registry code print? python class Plugin: registry = [] def init(self, name): self.registry.append(name) class…
- What does this Template Method code print? python class Base: def run(self): return self.a() + self.b() def a(self): return 'A' def…
- Why do many agile teams estimate story points on a Fibonacci-like scale (1, 2, 3, 5, 8, 13, …) instead of 1 to 10?
- Match each risk response with an example. P. Avoid Q. Transfer R. Mitigate S. Accept 1. Buy fixed-price insurance against data-centre…
- In an ATAM architecture evaluation, encrypting all messages between services improves security but raises latency. In ATAM terms, what is…
- A team rebases a long-lived branch every day and keeps resolving the same conflict in config.yml by hand. Which Git feature can do this…