📜  门| GATE-CS-2016(套装1)|第 46 题

📅  最后修改于: 2021-09-27 05:53:59             🧑  作者: Mango

当参数通过引用传递并假定动态范围时,以下伪代码的输出是什么?

a=3;
void n(x) {x = x * a; print(x);}
void m(y) {a = 1; a = y - a; n(a); print(a);}
void main() {m(a);} 

(一) 6, 2
(乙) 6, 6
(C) 4、2
(四) 4, 4答案: (D)
解释:

a = 3;

void main() 
{  
   // Calls m with 'a' being passed by reference
   // Since there is no local 'a', global 'a' is passed. 
   m(a);
} 


// y refers to global 'a' as main calls "m(a)"
void m(y) 
{
   // A local 'a' is created with value 1.
   a = 1; 

   // a = 3 - 1 = 2
   a = y - a; 

   // Local 'a' with value 2 is passed by 
   // reference to n()
   n(a); 

   // Modified local 'a' is printed
   print(a);
}

// Local 'a' of m is passed here
void n(x) 
{
  // x = 2 * 2 [Note : again local 'a' is referred
  //                   because of dynamic scoping]  
  x = x * a; 

  // 4 is printed and local 'a' of m() is also
  // changed to 4.
  print(x);
}

这个问题的测验