GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Trace local shadowing and nonlocal procedure bindings

3 questions solved the same way, newest first. Back to Programming in C

How these are solved

Each program has a global x, a P procedure with a local x, and Q using a nonlocal x. First identify the nonlocal binding under the stated scoping rule, then execute calls and parameter effects.

2 marks · MCQ
Report issue

Local shadowing and pointer update of a global

Scope, Lifetime & Storage Classes

Question 89, GATE 2003: Consider this C-style program (print emits its integer argument followed by a space):
int x;
void Q(int z) { z+=x; print(z); }
void P(int *y) {
  int x=*y+2;
  Q(x);
  *y=x-1;
  print(x);
}
main(void) { x=5; P(&x); print(x); }
What output is produced?
A. 12 7 6  B. 22 12 11  C. 14 6 6  D. 7 6 6
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. main sets the global x to 5 and passes its address, so y inside P points to that global object.
2. P declares a different, local x and initializes it to *y+2=5+2=7. Calling Q(x) copies 7 into Q's parameter z.
3. Q has no local x, so its x denotes the global, still 5. z becomes 7+5=12 and Q prints 12; this does not alter either x.
4. Back in P, *y=x−1 uses local x=7 on the right and writes 6 to the global through y. P prints its unchanged local 7.
5. main finally prints global x=6, producing 12 7 6, A. Passing the pointer allows the global update despite P's name shadowing.

Solution: main sets global x=5 and passes its address to P. P's local x becomes *y+2=7 and shadows the global only inside P. Q receives z=7; its unqualified x is the global 5, so z becomes 12 and Q prints 12. Returning to P, *y=x−1 writes 6 to the global, but P's local remains 7 and is printed. Finally main prints the global 6. The output is 12 7 6, A.

Original paper, page 13

Descriptive
Report issue

Trace static scope and value parameters

Scope, Lifetime & Storage Classes

Question 18(a), GATE 2000: Pseudo-Pascal: global x; Q(z) does z:=z+x; writeln(z). P(y) declares local x, does x:=y+2; Q(x); writeln(x). Main does x:=5; P(x); Q(x); writeln(x). Q and P are siblings declared in main. Find output under call-by-value and static scoping.
Question 18(a), GATE 2000: Pseudo-Pascal: global x; Q(z) does z:=z+x; writeln(z). P(y) declares local x, does x:=y+2; Q(x); writeln(x). Main does x:=5; P(x); Q(x); writeln(x). Q and P are siblings declared in main. Find output under call-by-value and static scoping.
Tap to enlarge
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.

Original paper, page 9

Descriptive
Report issue

Trace dynamic scope and reference parameters

Scope, Lifetime & Storage Classes

Question 18(b), GATE 2000: Pseudo-Pascal: global x; Q(z) does z:=z+x; writeln(z). P(y) declares local x, does x:=y+2; Q(x); writeln(x). Main does x:=5; P(x); Q(x); writeln(x). Q and P are siblings declared in main. Find output under call-by-reference and dynamic scoping.
Question 18(b), GATE 2000: Pseudo-Pascal: global x; Q(z) does z:=z+x; writeln(z). P(y) declares local x, does x:=y+2; Q(x); writeln(x). Main does x:=5; P(x); Q(x); writeln(x). Q and P are siblings declared in main. Find output under call-by-reference and dynamic scoping.
Tap to enlarge
Answer & solution

Answer: 14 14 10 10 (no official key was published)

Main’s x starts at 5. In P(x), y aliases main’s x, but P also owns a distinct local x. It reads y=5 and assigns local x=7. Q(x) is then called with a reference to that local x, so z aliases P.x. Under dynamic scoping, Q’s free x resolves to the nearest active caller with an x, also P.x. The assignment z:=z+x therefore reads 7+7 and writes 14 into P.x. Q prints 14, and P’s next writeln(x) prints the same 14. Main’s global x is still 5 because neither assignment changed y or main.x. After P returns, main calls Q(x); now both reference parameter z and dynamically resolved x refer to main.x. Their old values are 5, so the assignment writes 10 into main.x. Q prints 10 and main’s final writeln(x) also prints 10. Aliasing and name resolution are separate steps: first identify each referenced storage cell, then evaluate the addition using its pre-assignment value.

Original paper, page 9