Skip to content
Official PYQ · 1999

GATE CS 1999 Q2.13 — Programming in C

Explain with AI

Consider the following program in a language that has dynamic scooping:

var x: real;
procedure show:
    begin print(x);end;
procedure small;
    var x: real;
        begin x: = 0.125; show; end;
begin x:=0.25
    show; small
    end.

Then the output of the program is:

Choose one option
View worked solution

Answer: C

In a programming language with dynamic scoping, a free variable in a procedure is resolved by searching the execution call stack (the dynamic chain) backward from the point of the call until a definition for that variable is found.

Let's trace the execution of the program step-by-step:

  1. Global Initialization: The global variable x is declared. In the main block, x is assigned the value 0.25.
    Current active binding for x: Global x=0.25\text{Global } x = 0.25.
  2. First Call (show): The main block calls show.
    • Inside show, the variable x needs to be printed.
    • Looking back at the call stack: show ←\leftarrow main.
    • The most recent active binding for x is the global x, which has the value 0.25.
  3. Second Call (small): The main block calls small.
    • Inside small, a local variable x is declared and assigned the value 0.125.
    • Current active binding for x: Local x of small=0.125\text{Local } x \text{ of } \text{small} = 0.125.
  4. Nested Call (show from small): Inside small, the procedure show is called.
    • Inside show, we look up x.
    • Looking back at the call stack: show ←\leftarrow small ←\leftarrow main.
    • The most recent active binding for x in this chain is the local variable x inside small, which has the value 0.125.

Therefore, the output of the program is 0.25 0.125, which corresponds to Option C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions