edited by
2,104 views
1 votes
1 votes

What is the output of the following program if dynamic scoping is used?
 

int a,b,c;
void func1()
{
    int a,b;
    a=6;
    b=8;
    func2();
    a=a+b+c;
    print(a);
}

void func2(){
    int b,c;
    b=4;
    c=a+b;
    a+=11;
// here in case of dynamic scoping will 
//variable a of func1() be updated to a=6+11 ?
    print(c);
}

void main(){
    a=3;
    b=5;
    c=7;
    func1();
}
  1. 7 19
  2. 10 1 
  3. 10 23 
  4. 10 32
edited by

2 Answers

3 votes
3 votes

Initial values:
$a = 3,\quad b = 5,\quad c= 7$

In dynamic scoping when a function is called the called function gets the variable of the calling function even if not explicitly passed (and not the global one)

Here, in main, all $a,b,c$ are the global ones as there is no declaration here.

In func1:
New $a,b$ are declared. $a = 6, b = 8.$ func2 is called with these new values and the original global $c$.

In func2:
New $b,c$ declared. $b=4.$ $c = a + b$ assigns $6+4 = 10$ to $c$. $10$ will be printed and $a$ becomes $6+11 = 17$

In func1:
$a+b+c$ is printed which will be $17 + 8 + 7 = 32.$

0 votes
0 votes
But iam getting 7 ,21 as answer because in func1()  a,b are declared as local and local variables have higher precedence over global variables s0 6+8+7=21
Answer:

Related questions

3 votes
3 votes
2 answers
1
sunil sarode asked Nov 4, 2017
2,173 views
Choose the False statements:(a) The scope of a macro definition need not be the entire program(b) The scope of a macro definition extends from the point of definition to ...
1 votes
1 votes
2 answers
4