The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+39 votes
7.8k 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$
in Compiler Design by Loyal (7.1k points)
edited by | 7.8k 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.
+5
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

8 Answers

+83 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.

by Veteran (418k points)
edited by
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.
+10
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
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 ?
+11 votes

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

by (347 points)
0
best explaination
+2 votes
A is the answer
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
+2 votes

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.

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

by (53 points)
edited by
0 votes

The pseudo code can be written like this.

by Active (2.5k points)
0 votes
ans is 4,4
by (17 points)
–1 vote
Answer is B
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
Answer:

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
49,896 questions
55,153 answers
190,576 comments
85,317 users