📜  门|门 IT 2007 |第 34 题

📅  最后修改于: 2021-09-24 06:36:36             🧑  作者: Mango

考虑下面假设的编程语言中的程序,它允许全局变量和静态或动态范围的选择。

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

设 x 是静态范围下打印的值,y 是动态范围下打印的值。那么,x 和 y 是
(A) x = 10, y = 10
(B) x = 20, y = 10
(C) x = 10, y = 20
(D) x = 20, y = 20答案: (C)
说明:静态范围:

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
} 

这个问题的测验
如果您发现上面的帖子有任何错误,请在下面评论