GATE | GATE-CS-2001 | Question 44
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.
If the language has dynamic scoping and parameters are passed by reference, what will be printed by the program?
(D) None of the above
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