# GATE2001-2.19

2k views

Consider the following program

Program P2
var n : int;

procedure W(var x : int)
begin
x = x + 1;
print x;
end

procedure D
begin
var n : int;
n = 3;
W(n);
end

begin    \\begin P2
n=10;
D;
end

If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program?

1. 10
2. 11
3. 3
4. None of the above

edited

ans is D.

here, because of dynamic scoping the value of n passed in w(n) will be n=3.

therefore o/p will be 4, which is not in any option.

selected by
0
dynamic scoping part is understood by me the answer will be 4..but does parameters are passed by reference mean the below when converted to C language.

int n;

W(int *x) {*x = *x + 1; print *x;}

D(){int n; n = 3; W(&n); }

main(){n=10; D(); }
3

You are correct  Nitika Gupta.Internally how it will change it does not matter,What matters in the actual value of n will be changed by which is passed by D to W

Also, in the answer it says that

here, because of dynamic scoping the value of n passed in w(n) will be n=3.

I dont think dynamic scoping will have any role here,because as long as we have local variable(x),it will be used.Scoping comes into picture,when variable getting used is not defined in the scope of the function which is using it.

0
Even if you run it in C, the answer will be 4. C uses static scoping if anyone wants to check.
It print 4 , which is not in option,

So correct option is D
n=3
W(n)=W(3)
Procedure W(var x; int)
begin
x = x+1 = 3+1 = 4
Print x → Print x=4
end

Hence option D is correct
0
I think it  will print 5

it is passing parameters by reference,1. the first function call is W(&n)(passing reference given in the question) so *x=*x+1 it will print 4 and the value of n is changed into 4

now the 2nd function call is D where it will call W(&n) again, then *x=*x+1 so it will print 5.

correct me if I'm wrong

## Related questions

1
3.9k views
What is printed by the print statements in the program $P1$ assuming call by reference parameter passing? Program P1() { x = 10; y = 3; func1(y,x,x); print x; print y; } func1(x,y,z) { y = y + 4; z = x + y + z } $\text{10, 3}$ $\text{31, 3}$ $\text{27, 7}$ None of the above
Consider the following program in pseudo-Pascal syntax. What is printed by the program if parameter $a$ in procedure test1 is passed as call-by-reference parameter call-by-value-result parameter program Example (input, output) var b: integer; procedure test2: begin b:=10; end procedure test1 (a:integer ... ('point 2: ', a, b); end begin (*Example*) b:=3; test1(b); writeln('point3: ', b); end