Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

GATE | GATE-CS-2001 | Question 44

  • Last Updated : 28 Jun, 2021

Consider the following program

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.




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

My Personal Notes arrow_drop_up
Recommended Articles
Page :