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)
  1. A. It prints ['a>1', 'LIMIT 5', 'b<2'] in call order
  2. B. It prints ['a>1', 'b<2', 'LIMIT 5'], clauses first
  3. C. AttributeError, since limit returns None
  4. 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'.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic