in Programming edited by
5,824 views
15 votes
15 votes

Consider the program below in a hypothetical programming language which allows global variables and a choice of static or dynamic scoping.

int i;
program main()
{
    i = 10;
    call f();
}

procedure f()
{   
    int i = 20;
    call g ();
}
procedure g()
{   
    print i;
}

Let $x$ be the value printed under static scoping and $y$ be the value printed under dynamic scoping. Then, $x$ and $y$ are:

  1. $x=10, y=20$
  2. $x=20, y=10$
  3. $x=10, y=10$
  4. $x=20, y=20$
in Programming edited by
5.8k views

4 Comments

@Arjun Sir

the selected best answer itself says that x=10 and y=20...then why option C is correct which says x=20 and y=20 ?
0
0

@Arjun sir

I think its " In static scoping, if a variable is not defined in the local space, it is looked for in the next higher block...(recursively) and finally in the global space.
Is that correct?

0
0
please correct the answer to option A
0
0

3 Answers

29 votes
29 votes
Best answer

In static scoping, the scope of an identifier is determined by its location in the code, and since that doesn't change, the scope doesn't either. In dynamic scoping, the scope is determined by the sequence of calls that has led to the use of an identifier, and since that can be different each time that use is reached, is dynamic.

So, here:

Option A must be the answer.

As, under static scoping$:x=10(\text{global}\; i)$

under dynamic scoping$:y=20($according to the sequence of calls,i.e $20)$

edited by

3 Comments

Option A is answer:--

Whatever be the scoping, the i declared inside the method f() is not accessible outside. So when we say i from g(), it refers to global i. So under both cases, 10 is printed

–3
–3
@Sumit

u r saying under static scoping x=10 and under dynamic scoping y=20...i agree with this but then why option C? option C says x=20 and y=20...

whats wrong ?
3
3
yes, its option A.
3
3
7 votes
7 votes
The answer should be (A) x = 10 and y = 20,

Since the value of x is based on static scoping, in the procedure g() print i will directly look into the global scope and find i = 10 which was previously set by main() and

Since the value of y is based on dynamic scopint, procedure g() will first look into the function which called it, i.e. procedure f() which has a local i = 20, which will be taken and 20 will be printed.
–2 votes
–2 votes
In main procedure value of i is 10

Now in static scoping when main is called global variable i becomes 10.

then f() called , global variable i value becomes 20

Now g() is called. g() has no local variable of it's own . So, it takes the value of global i , and prints i=20

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now in dynamic scoping main is called , global variable i becomes 10.

then f() called, but it won't change the value of global variable i .

Now g() called and here i is not declared, So, it takes the value from previous scope of f() , i.e. i=20

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

So, Ans C) 20,20

4 Comments

wrt to ur quotation...

then f() called , global variable i value becomes 20

See, i here is local variable not global.

0
0

yes, that will not change global i

but if i is locally not declared but evaluating it's value from some other variable, then i value changes

right?

https://gateoverflow.in/42555/scope

here in my command I asked that. That is why I told u to see it

Pretty good question

0
0

your question--->>>but if i is locally not declared but evaluating it's value from some other variable, then i value changes??

Ans.:- Yes if i is not locally declared  but evaluating it's value from some other PLACE, then Global i will change.

Plz refer this:->>https://gateoverflow.in/19381/what-the-difference-between-static-scoping-dynamic-scoping

0
0

https://gateoverflow.in/972/gate2003-89

" Yes if i is not locally declared  but evaluating it's value from some other PLACE, then Global i will change."

not maintaining here then 

in main x=5

global x=5

Now, after calling P() value of x will be 7, then global x also 7(as per  your assumption)

But then z will print 14

But z is printing 12.

What problem is here?

0
0
Answer:

Related questions