Processing math: 100%

Quiz 3: Pre/Postcondition Change

1. Which condition is weaker than x>0?

  • 2x>0
  • xx>0
  • x+y>0
  • x>1

Correct answer: xx>0


Explanation:

Recall: P1P2 means the conditions are equivalent.

  • x>02x>0, so the conditions are equivalent.
  • x>0xx>0, so x>0 is stronger.
  • x>0 and x+y>0 aren't comparable because y isn't included in the implicant.
  • x>1x>0, so x>1 is stronger.

2. Which statement is correct?

  • length=count is stronger than length=(count1)
  • length=count is neither stronger nor weaker than length=(count1)
  • length=count is weaker than length=(count1)
  • length=count is equivalent to length=(count1)

Correct answer: length=count is neither stronger nor weaker than length=(count1)


Explanation:

It doesn't make sense to consider the implication relationship length=countlength=(count1) or vice-versa. Thus, there is no relationship between their contract strengths.

3. Which of the following is the most reasonable precondition of the method deposit(double amount) in the BankAccount class?

  • amount0
  • amount0
  • amount is any double value
  • amount>0

Correct answer: amount>0


Explanation:

The action of "depositing" at a bank only makes sense when you're depositing a positive amount of money. This rules out all but amount>0.

4. In the following code, Module B results in a call of sqrt(5). Which statement is correct?

// Module A // Precondition: x >= 0 // Postcondition: return x's square root double sqrt(double x) { ... }
// Module B double x = -5; double y = sqrt(x); assert abs(y * y-x) < eps
  • There is no bug in either A or B
  • There is a bug in both A and B
  • There is a bug in A
  • There is a bug in B

Correct answer: There is a bug in B


Explanation:

Recall the Pre/Postcondition Violation Rules.

Client B violates Supplier A's precondition, so the client (B) is at fault.