CS FundamentalsVery hardGATE CS

For integer x, what is the weakest precondition wp(if x > 0 then y := x else y := −x, y ≥ 2)?

  1. A. x ≥ 2
  2. B. x ≥ 2 or x ≤ −2
  3. C. x > 0 and x ≥ 2
  4. D. true

Correct answer: B. x ≥ 2 or x ≤ −2

wp(if B then S1 else S2, Q) = (B ∧ wp(S1, Q)) ∨ (¬B ∧ wp(S2, Q)) = (x > 0 ∧ x ≥ 2) ∨ (x ≤ 0 ∧ −x ≥ 2), i.e. x ≥ 2 or x ≤ −2 (|x| ≥ 2). Keeping only the then-branch gives x ≥ 2.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic