📜  门| GATE-CS-2016(Set 1)|第46章

📅  最后修改于: 2021-06-30 00:13:45             🧑  作者: 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);} 

(A) 6、2
(B) 6、6
(C) 4、2
(D) 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);
}

这个问题的测验