GATE | GATE-CS-2001 | Question 44

Consider the following program

filter_none

edit
close

play_arrow

link
brightness_4
code

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 

chevron_right


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



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.