Open In App

GATE | GATE-CS-2001 | Question 44

Like Article
Like
Save
Share
Report

Consider the following program




Program P2 
    var n: int: 
     procedure W(var x: int) 
     begin 
         x=x+1
         print x;   
     end 
  
     procedure
     begin  
         var n: int; 
         n=3
         W(n);  
     end 
begin //beginP2 
  n=10
  D; 
end 


If the language has dynamic scoping and parameters are passed by reference, what will be printed by the program?
(A) 10
(B) 11
(C) 3
(D) None of the above


Answer: (D)

Explanation:

In static scoping or compile-time scoping the free variables (variables used in a function that are neither local variables nor parameters of that function) are referred as global variables because at compile only global variables are available.
In dynamic scoping or run-time scoping the free variables are referred as the variables in the most recent frame of function call stack. In the given code in the function call of procedure W the local variable x is printed i.e 4. Under dynamic scoping if x would have not been there in procedure W then we would refer to x of the function in function call stack i.e procedure D and the main function but since x is a local variable not a free variable we referred to the local variable hence 4 will be printed.

See question 4 of https://www.geeksforgeeks.org/principle-of-programming-languages-set-1/

This solution is contributed by Parul Sharma.

Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads