GATE Computer ScienceHardGATE CS (PYQ pattern)

int j = 2; int A[0..4] = {0, 3, 1, 4, 2}; int Sum(k, lo, hi, e) { int s = 0; for (k = lo; k <= hi; k++) s = s + e; return s; } All parameters are passed by name. What does Sum(j, 1, 4, A[j] * j + 1) return?

  1. A. 26
  2. B. 33
  3. C. 29
  4. D. 38

Correct answer: C. 29

k is the caller's j and e is re-evaluated at every use, so the loop adds A[j]*j + 1 for j = 1..4: 4 + 3 + 13 + 9 = 29. A single evaluation at j = 2 would give 3, times 4.

More Compiler Design questions

Exams that ask this topic