Variable Binding

Question 1

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

A
x = 10, y = 10
B
x = 20, y = 10
C
x = 10, y = 20
D
x = 20, y = 20
Question 1 Explanation: 
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 scope, 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.
There is 1 question to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access