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?
- A. 26
- B. 33
- C. 29
- 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
- An SLR(1) shift-reduce parser for E → E + T | T; T → T F | F; F → ( E ) | id parses "( id + id ) id". Each shift and each reduce counts as…
- An LR parser for S → S ( S ) | ε parses "( ( ) ) ( )". Each shift and each reduce (including reductions by S → ε) counts as one move. What…
- Minimal SSA form (φ-functions at the iterated dominance frontier of each variable's defining blocks, no pruning) is built for 1: x = 1; 2…
- SSA form is built for 1: i = 0; 2: L1: if i >= n goto L4; 3: if i > k goto L2; 4: x = i 2; 5: goto L3; 6: L2: x = i + 1; 7: y = x; 8: L3…
- Minimal SSA form without liveness pruning (φ-functions at the iterated dominance frontier of every block that assigns the variable) is…
- In 1: a = 1; 2: b = 2; 3: if p > 0 goto L1; 4: a = a + 1; 5: goto L2; 6: L1: b = b + 1; 7: L2: c = a + b; 8: if c > q goto L3; 9: a = c…