GATE Computer ScienceModerateGATE CS (PYQ pattern)

int x = 1; int a() { return x; } int b(int n) { int x = n * 10; if (n == 0) return a(); return b(n - 1) + a(); } What does b(2) return under static scoping and under dynamic scoping respectively?

  1. A. 30 and 3
  2. B. 3 and 30
  3. C. 3 and 10
  4. D. 4 and 30

Correct answer: B. 3 and 30

Static: a() always reads the global 1, so b(0) = 1, b(1) = 2, b(2) = 3. Dynamic: a() reads the x of the innermost active b: b(0) = 0, b(1) = 0 + 10, b(2) = 10 + 20 = 30.

More Compiler Design questions

Exams that ask this topic