GATE Computer ScienceHardGATE CS (PYQ pattern)

Each assignment is a definition named by its statement number. In 1: s = 0; 2: i = 0; 3: L1: if i >= n goto L4; 4: if a > 0 goto L2; 5: s = s + i; 6: goto L3; 7: L2: s = s - i; 8: a = a - 1; 9: L3: i = i + 1; 10: goto L1; 11: L4: return s, how many definitions reach the entry of statement 9 (L3), and how many reach statement 11 (L4)?

  1. A. 6 and 6
  2. B. 4 and 5
  3. C. 5 and 6
  4. D. 5 and 5

Correct answer: C. 5 and 6

At L3: s comes from 5 or 7 (s = 0 at 1 is killed on both paths into L3); i from 2 (first iteration) and 9 (around the loop); a from 8 (on the L2 path): {2, 5, 7, 8, 9} = 5. At L4 the loop may run zero times, so s = 0 (1) also reaches, together with 2, 5, 7, 8, 9: 6.

More Compiler Design questions

Exams that ask this topic