The Gateway to Computer Science Excellence
+47 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 by
edited by | 10k views
Here the information about datatype is not given so how will complier know which type of memory to assign?
we need not know what data type it is. it is said to be a pseudo code in question and dynamic scoping will work the same way for all data types. can you please elaborate on your confusion regarding type of memory allocated?
Ans will be 4,4
@srestha after printing 4, control comes back to the context of function m, isn't it ? In that it is printing a. how it is 4 here?

edit:  got it .. thanks.
a = 3; in this program equivalent to int a=3:  Gloabal variable

a = 1; in this program equivalent to int a=1: Local variable

@prashant understood.. thanks for clarifying.
@prashant.. Y only int?
you can assume anything. type will give you information about how much memory it is going to allocate.
What is the output if parameters are passed by value?

if dynamic scoping and call by value is used then answer would be


data type declaration is not required at all. Just solve this code like you are solving the code written in python where no data type is used.(However do not co-relate the syntax in python  with this syntax)
This is was the actual confusion. Thanks @prashant
Is Dynamic Scoping in current GATE Syllabus?

8 Answers

+100 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
Sir , how can you say that a=1 is a variable declaration....? Sir it may be assignment.......
if its assignment, answer comes out to be 0,0 which is clearly not in options.. so it should be declaration only.. its a little ambiguous actually..
Yes. And also "a=3" is also given without a type and must be a declaration.
But it is given parameters are passed by reference, in that case won't the answer be 6,6?

@arjun sir, I don't get your last sentence - "Being passed by reference, "a" in m() also get updated to 4. So, D is the answer here."

The value of x got updated to 4. How will if affect the value of local var. a in the calling function ?

How would the answer be 0,0 then?
@ravi You are thinking in terms of C language where there is no dynamic scoping. If a language is having dynamic scoping, unless a variable is defined (given memory) in a function, the memory of that variable comes from whichever function that called it and this goes on recursively and finally global definitions are considered.
yes sir, I read about dynamic scoping, now things are clear :) Thankyou very much :)
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); print(a) }

now what is output ??

@gabbar  answer will be 4,4,3

I think it will be 4,4,3 becoz in main we will print global a(whch is not changed in m()) not it?
@KISHALAY  yes 4 4 3 should be ans
How you can say that a is local for a=1 in m(y) , it may also me possible that it is assigning 1 to global a.
However if we do not consider local a then no option will be matched . SO i think it is an ambiguous question.
Dynamic scoping implementation

1) Search for the local variable.

2)If not found look for calling stack for defination

3)If not in calling stack also then use global

void n(x) {x=x*a;print(x);}

a is not defined in n(x) step 1 fail goto 2

a is defined in m(y) return that value of a.

hence x=2*2 print 4
No new variables 'a' and 'x' in function m() and n() respectively are declared & defined. So can't we take all variables x, y, a pointing to single variable a only as a's address is passed everywhere.
In case of static scoping, output= 66

dynamic scoping = 44

Question is asked with dynamic scoping.
for the same question:

 1) CBV and Static Scoping: 6,2

 2) CBR and Dynamic Scoping: 6,6

is this correct plz verify it??

If we need to change global a from function m(), how to do that? 

Nice explanation.This answer is as per the question as well as logic.

@ravi_ssj4 It's mentioned in the question that parameters are passed by reference so when we did x = x * a. The value of x ultimately updates the value of a as well (in the scope chain).

can anyone pls resolve this , why is var 'a' inside m() considered as local ? It is not declaration ,it is an assignment ,right ?
+12 votes

May be this works ............

best explaination
+8 votes

Nicely explained here

+2 votes
A is the answer

i got a too
i got the answer A but the problem with this question is Scoping and Parameter passing mechanism are points which are removed from syllabus of gate 2016 if u dont believe me u can see gate 2015 and gate 2016 sylabus in 2015 it is explicitly mentioned but not in gate 2016 dont fight for this question everybody is going to get 2 marks for free
+1 vote

it is D;


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
+1 vote

The pseudo code can be written like this.

0 votes
ans is 4,4
–1 vote
Answer is B
int a=3;
void n(int *x){
void m(int *y){
	int a=1;
void main(){

// OUTPUT: 6 6
Is C dynamically scoped or statically scoped?
Does C pass parameters by value or by reference.?

Answer the above two questions, and you would know why you are wrong.
It is given in the question that parameters are passed by reference
U have run this code on c compiler and c uses static scoping and Pass by value mechanism.
You got 66  because here C++ used static scoping and in question it is given to use dynamic scoping.
C by default uses STATIC scope and PASS BY VALUE but not call by reference

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,345 questions
60,497 answers
95,314 users