Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

GATE | Gate IT 2007 | Question 34

  • Difficulty Level : Easy
  • Last Updated : 28 Jun, 2021

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

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.




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


Answer: (C)

Explanation: Static scoping:




int i ;
program main ()
{
    i = 10;
    call f();
}
  
procedure f()
{   
    int i = 20;
    call g ();
}
procedure g ()
{   
    print i; //as i=20 is scoped only within f() so it will point to global i
}
  So, 10 is printed
Dynamic scoping:
int i ;
program main ()
{
    i = 10;
    call f();
}
  
procedure f()
{   
    int i = 20; // here global scoped i is changed
    call g ();
}
procedure g ()
{   
    print i; // global value changed so, i=20 printed


Quiz of this Question
Please comment below if you find anything wrong in the above post




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!