in Compiler Design edited by
23,918 views
69 votes
69 votes

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?                

a = 3;
void n(x) { x = x * a; print (x); }
void m(y) { a = 1 ; a = y - a;  n(a); print (a); }
void main () { m(a); }
  1. $6,2$ 
  2.  $6,6$ 
  3.  $4,2$ 
  4.  $4,4$
in Compiler Design edited by
23.9k views

4 Comments

Is Dynamic Scoping in current GATE Syllabus?
1
1
yes
0
0

. Dynamic Scoping – If variable is not defined in any function than it take value from previous function who called that function.

. Static Scoping - If variable is not defined in current function than it take value from global variable if there is no global variable than throw error.

. Passed by Reference – same as passing the address if we change value in current function than it got changed in previous function. 

By default C support   Static Scoping.

6
6

4 Answers

138 votes
138 votes
Best answer

It is a bit confusing as variable declaration is not explicit. But we can see that "$a=3$" and "$a=1$" are declaring new variables, one in global and other in local space.

Main is calling $m(a)$. Since there is no local '$a$', '$a$' here is the global one.

In m, we have "$a = 1$" which declares a local "$a$" and gives $1$ to it. "$a = y-a$" assigns $3-1 = 2$ to '$a$'.

Now, in $n(x)$, '$a$' is used and as per dynamic scoping this '$a$' comes from '$m()$' and not the global one. So, "$x=x*a$" assigns "$2*2 = 4$" to "$x$" and $4$ is printed. Being passed by reference, "$a$" in $m()$ also get updated to $4$. So, D is the answer here.

edited by
by

4 Comments

thank you, i was confused because local variable is not declared as int a; in m(y).
0
0
Here in the question mentioned it is a pseudo code , inside the function int a=1 not declare so because of it's not a actual code and if we take variable a(inside m(y) function) no option match so taking this as a local variable of function m();

correct me if i am wrong
0
0
Here in the question mentioned it is a pseudo code , inside the function int a=1 not declare so because of it's not a actual code and if we take variable a(inside m(y) function) no option match so taking this as a local variable of function m();
0
0
4 votes
4 votes

it is D;

Explanation:::

in call by reference value of actual variable can be changed as we have got its address
in Dynamic scoping free variable are resolved from previous function call.

main() is calling m(a); thus a call m(addressof_a) will be made.

now coming m(y),  a's address value will be copied to *y. so now inside m() we have local variable a=1 and y=&a

"a=y-a" will update the local a's value to 2. now m() calls n() by passing address_of_local_variable_a.

coming to n(a),  a's address value will be copied to *x. Now inside n() there is no local variable 'a' is defined/declared. so now it will look for a's value into upper/previous function i.e. m(). since a local variable 'a' is defined into m(). So its value a=2 will be taken. 
note that here x is a pointer pointing to variable 'a' which was declared in m().

now (*x)=(*x)*a will evaluated as 4 which will update the value of 'a' declared in m(). so now coming to print statement address_of_a is passed as argument which will print 4.

after n() calling is over m() will return back to the position where it left and continue just after next statement. which print a's updated value i.e. 4

so finally in output we will have 4,4

edited by
3 votes
3 votes

The pseudo code can be written like this.

0 votes
0 votes
Answer is B

4 Comments

U have run this code on c compiler and c uses static scoping and Pass by value mechanism.
1
1
You got 66  because here C++ used static scoping and in question it is given to use dynamic scoping.
0
0
C by default uses STATIC scope and PASS BY VALUE but not call by reference
0
0
Answer:

Related questions