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?

  1. A. The caller's: the client violated the precondition; the supplier may assert it to fail fast rather than silently 'fix' the input
  2. B. The method's: it must accept every input
  3. C. Nobody's: contracts are documentation only
  4. 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.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic