GATE Computer ScienceHardGATE CS (PYQ pattern)
int x = 1; void p() { x = x + 10; } void r() { print(x); } void q() { int x = 5; p(); r(); } main() { q(); r(); print(x); } What is printed (three values in order) under static scoping and under dynamic scoping?
- A. static: 5 11 11; dynamic: 15 1 1
- B. static: 11 11 11; dynamic: 15 15 1
- C. static: 11 1 1; dynamic: 15 1 11
- D. static: 11 11 11; dynamic: 15 1 1
Correct answer: D. static: 11 11 11; dynamic: 15 1 1
Static: p and r always see the global x. p makes it 11; r inside q prints 11; r from main prints 11; main prints 11. Dynamic: called from q, p finds q's local x = 5 and makes it 15; r inside q prints 15. After q returns its x is gone, so r from main finds the global, still 1, and main prints 1.
More Compiler Design questions
- Each assignment is a definition named by its statement number. In 1: s = 0; 2: i = 0; 3: L1: if i >= n goto L4; 4: if a > 0 goto L2; 5: s…
- A basic block is converted to a DAG using value numbering, treating + and as commutative, reusing an existing node when the same operator…
- A web of a variable is a maximal union of def-use chains that share a definition or a use; a register allocator gives each web its own…
- In 1: x = 1; 2: y = 0; 3: L1: if y > n goto L3; 4: if x > 5 goto L2; 5: x = x 2; 6: L2: y = y + x; 7: x = y - 1; 8: if y < m goto L1; 9: x…
- Using the standard backpatching scheme (each relational test yields 'if a<b goto ' followed by 'goto ', with no fall-through optimisation…
- For S → a A | A b; A → c | ε (augmented), how many non-blank ACTION entries does the initial state I0 have in the SLR(1) table, and in the…