GATE 1987
June 2, 2024GATE 1987
June 2, 2024Question 10619 – GATE 1987
Study the following program written in a block-structured language:
Var x, y:interger; procedure P(n:interger); begin x:=(n+2)/(n-3); end; procedure Q Var x, y:interger; begin x:=3; y:=4; P(y); Write(x) __(1) end; begin x:=7; y:=8; Q; Write(x); __(2) end.
What will be printed by the write statements marked (1) and (2) in the program if the variables are statically scoped?
Correct Answer: A
Question 29 Explanation:
First procedure Q is called from the main procedure. Q has local variables x and y with values 3 and 4 respectively. This local variable y (value 4) is being parsed to procedure P during call, and received in local variable n inside procedure P. Now as P does not have any local definition for variable x, it will assign the evaluated value of (n+2)/(n-3), i.e., (4+2)/(4-3)=6 to the global variable x, which was previously 7. After the call of procedure P, procedure Q writes the value of local variable x which is still 3. Lastly, the main procedure writes the value of global variable x, which has been changed to 6 inside procedure P. So, the output will be 3, 6.
3, 6
6, 7
3, 7
None of the above.