CS FundamentalsHardProduct-company design interview
Under design by contract, a method sqrt(x) has the precondition x ≥ 0. A caller passes -4 and the method fails. Whose bug is it, and which practice follows from this view?
- A. The caller's: the client violated the precondition; the supplier may assert it to fail fast rather than silently 'fix' the input
- B. The method's: it must accept every input
- C. Nobody's: contracts are documentation only
- D. The compiler's: it should have rejected the call
Correct answer: A. The caller's: the client violated the precondition; the supplier may assert it to fail fast rather than silently 'fix' the input
Contracts divide responsibility: clients guarantee preconditions, suppliers guarantee postconditions and invariants. Checking a precondition with an assertion catches the client's bug at its source.
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 service catches every exception in a broad try/except, logs nothing and returns an empty list, so a database outage looks like 'no…
- How many of these defend against SQL injection in a login query: (1) parameterised queries / prepared statements, (2) escaping quotes by…
- A document-editor framework defines an abstract Application class with a method newDocument() that calls createDocument(); each concrete…
- An object adapter and a class adapter both make class Legacy fit interface Target. Which statement is correct?
- Both Facade and Mediator sit between groups of objects. What is the essential difference?
- A Customer may or may not have a discount policy. Instead of writing if policy is not None: total = policy.apply(total) everywhere, the…