CS FundamentalsVery hardGATE CS

Which Hoare triple is valid for integer variables (partial correctness)?

  1. A. {x > 0} y := x * x {y > x}
  2. B. {true} y := x * x {y > 0}
  3. C. {x > 0} y := x - 1 {y > 0}
  4. D. {x ≥ 2} y := x * x {y ≥ 2 * x}

Correct answer: D. {x ≥ 2} y := x * x {y ≥ 2 * x}

x² ≥ 2x holds exactly when x ≥ 2 or x ≤ 0, so the last triple holds. The others fail at a boundary: x = 1 gives y = x in the first and y = 0 in the third, and x = 0 gives y = 0 in the second.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic