Similar questions 2 more →
Scope, Lifetime & Storage Classes


Answer & solution
Answer: 12 7 10 5 (no official key was published)
Main sets global x=5. P receives a separate value parameter y=5 and sets its local x=y+2=7. P calls Q(x), so Q receives a separate z=7. Static scoping resolves Q’s free x through its lexical declaration environment, main, not through the caller P: that x is the global 5. Therefore Q sets its own z=7+5=12 and prints 12. Because z was passed by value, P’s local x remains 7, so P next prints 7. Returning to main leaves global x=5. Main’s Q(x) receives z=5 and uses global x=5, so it prints 10; this changes only z. Finally main prints global x=5. The four outputs, one per writeln, are 12,7,10,5. Do not let P’s local x affect a statically scoped sibling procedure.