8.1k views

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$

edited | 8.1k views
0
Here the information about datatype is not given so how will complier know which type of memory to assign?
0
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?
0
Ans will be 4,4
0
@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.
+7
a = 3; in this program equivalent to int a=3:  Gloabal variable

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

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

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

4,2
0
@kamakshi

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)
0
This is was the actual confusion. Thanks @prashant
0
Is Dynamic Scoping in current GATE Syllabus?

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.

by Veteran (423k points)
edited by
0
@arjun
Sir , how can you say that a=1 is a variable declaration....? Sir it may be assignment.......
+11
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..
+7
Yes. And also "a=3" is also given without a type and must be a declaration.
0
But it is given parameters are passed by reference, in that case won't the answer be 6,6?
+4

@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 ?

+4
How would the answer be 0,0 then?
+11
@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.
0
yes sir, I read about dynamic scoping, now things are clear :) Thankyou very much :)
+5
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 ??

0
+2
@Gabbar,

I think it will be 4,4,3 becoz in main we will print global a(whch is not changed in m())...is not it?
0
@KISHALAY  yes 4 4 3 should be ans
0
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.
+11
Dynamic scoping implementation

1) Search for the local variable.

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
0
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.
+2
In case of static scoping, output= 66

dynamic scoping = 44

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

1) CBV and Static Scoping: 6,2

2) CBR and Dynamic Scoping: 6,6

is this correct plz verify it??
0

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

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

@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).

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

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

by (477 points)
0
best explaination
by (141 points)
0
sure
?

i got a too
0
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 .....so dont fight for this question everybody is going to get 2 marks for free

Nicely explained here

by Active (1.8k points)
+1 vote

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.

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

by (59 points)
edited

The pseudo code can be written like this.

by Active (3.2k points)
ans is 4,4
by (17 points)
–1 vote
by Junior (701 points)
–1
#include<stdio.h>
#include<conio.h>
int a=3;
void n(int *x){
*x*=a;
printf("%d",*x);
}
void m(int *y){
int a=1;
a=*y-a;
n(&a);
printf("%d",a);
}
void main(){
m(&a);
}

// OUTPUT: 6 6
+2
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.
0
It is given in the question that parameters are passed by reference
+1
U have run this code on c compiler and c uses static scoping and Pass by value mechanism.
0
You got 66  because here C++ used static scoping and in question it is given to use dynamic scoping.
0
C by default uses STATIC scope and PASS BY VALUE but not call by reference