1,867 views
4 votes
4 votes
Indicate results of the following program if the language uses
i)Static scope rule and
ii) Dynamic scope rules (GATE-1989)
var x,y:integer;
procedure A(var z:integer);
var x:integer;
begin
x:=1;
B;
z:=x;
end;
procedure B;
begin
x:=x+1;
end;
begin
x:=5;
A(y);
write(y)
end;

4 Answers

3 votes
3 votes
If call by value is used. It must be 0 in both the cases.

Since, Global variables are initialized with 0 and since we are not changing value of y anywhere in the program, it will remain same.
3 votes
3 votes

In static scoping value will be 6,And in Dynamic scoping value will be 2

0 votes
0 votes

In static scoping: 1

In dynamic scoping: 2

if call by reference is used then in static scoping when the function B is called inside the function A 

the statement x= x+1 will change the global value of x = 5  6

but the main function is never using this global value of x it is printing the value of actual parameter y which is the formal parameter z in function A.

and z = 1, so, 1 will be printed in the case of static scope.

and when Dynamic scoping  is used and the function  B is called inside function A 

the statement x= x+1 will change the value of the variable x present in the calling function i.e. A, so X= 1 2

Related questions

1 votes
1 votes
1 answer
1
sushmitha asked Sep 10, 2018
667 views
can anyone explain by using static and dynamic programing ? what will be output for static and dynamic? #include<stdio.h>int x=10;void part1(int *a){*a+=x++;printf("%d"...
0 votes
0 votes
0 answers
2
saumya mishra asked Dec 16, 2017
931 views
Can anyone please explain what is the answer to this question by static and dynamic scoping?
7 votes
7 votes
3 answers
4