GATE Computer ScienceHardGATE CSE
Program: int x = 5; int f() { return x; } int g() { int x = 7; return f() + x; } int h() { int x = 11; return g() + f() + x; } main calls h(). What does h() return under static (lexical) scoping and under dynamic scoping respectively?
- A. 36 and 28
- B. 28 and 28
- C. 28 and 36
- D. 30 and 34
Correct answer: C. 28 and 36
Static: f always sees the global x=5, so g = 5+7 = 12, h = 12+5+11 = 28. Dynamic: f sees the caller's x: g = 7+7 = 14, f in h sees 11, h = 14+11+11 = 36.
More Compiler Design questions
- The expression ((((a + b) + c) ((a + b) + d)) - ((a + b) ((a + b) + c))) is stored as a DAG with common subexpressions shared (no…
- Control-flow graph: B1: a=1; b=2; c=a+b; then B2: d=c+b; if d<10 goto B4 (else B3); B3: c=d+a; b=c; goto B2; B4: print(c+a). Edges: B1→B2…
- Let L = { w ∈ {a,b} : the 4th symbol from the right end of w is 'a' }. The minimal DFA for L has how many states (including any dead/trap…
- Procedure P(x, y) { x = x + 1; y = x + y; } is called as P(a, a) with a = 3. What is a after the call under call-by-reference, and under…
- Grammar: S → L = R | R; L → R | id; R → L (augmented with S' → S). How many states do the LR(0) automaton and the canonical LR(1)…
- Grammar: S → a A d | b B d | a B e | b A e; A → c; B → c. How many states does the canonical LR(1) collection contain, how many does…