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?

  1. A. 36 and 28
  2. B. 28 and 28
  3. C. 28 and 36
  4. 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

Exams that ask this topic