GATE Computer ScienceHardGATE CS (PYQ pattern)
int x = 1; int g() { x = x + 1; return x; } int f(int n) { int x = n; if (n == 0) return g(); int r = f(n − 1); return r + g(); } What does f(3) return under static scoping and under dynamic scoping?
- A. 14 and 6
- B. 10 and 14
- C. 14 and 10
- D. 9 and 10
Correct answer: C. 14 and 10
Static: g always increments the global x: f(0) gets 2, f(1) = 2 + 3 = 5, f(2) = 5 + 4 = 9, f(3) = 9 + 5 = 14. Dynamic: g increments the x of the f that called it: f(0): 0 → 1, returns 1; f(1): 1 + (1 → 2) = 3; f(2): 3 + 3 = 6; f(3): 6 + 4 = 10 (the global x stays 1).
More Compiler Design questions
- The standard algorithm removes all left recursion from S → A a | b; A → A c | S d | e, taking the non-terminals in the order S, A…
- int i = 1; int A[0..2] = {5, 6, 7}; void f(x, y) { x = x + 1; y = 10; } The call f(i, A[i]) uses copy-in/copy-out (value-result), copying…
- For G: S → x S y | x y | S S, which statements are true? I. G is ambiguous. II. The canonical LR(1) table has more multiply-defined cells…
- A lexer uses longest match, breaking ties in favour of the rule listed first, with rules: KW = if | then; ID = [a-z][a-z0-9]; NUM =…
- G: S → id | id [ E ] | id ( E ); E → id. G′ is obtained from G by left factoring S. Which statements are true? I. G is LL(1). II. G′ is…
- Construct the SLR(1) parsing table of S → a B | b A; A → a | a S | b A A; B → b | b S | a B B (augmented). In how many states of the…