GATE | GATE-CS-2006 |问题56

考虑下面的代码,如Fortran和代码中的这些语句。

null
subroutine swap(ix,iy)
     it = ix
L1 : ix = iy
L2 : iy = it
end
  ia = 3
  ib = 8
  call swap (ia, 1b+5)
  print *, ia, ib
end 

S1:编译器将生成代码来分配一个临时的无名单元,将其初始化为13,并传递单元交换的地址 S2:执行时,代码将在L1行生成运行时错误 S3:执行时,代码将在第L2行生成运行时错误 S4:程序将打印13和8 S5:程序将打印13和-2 以下语句集完全正确: (A) S1和S2 (B) S1和S4 (C) S3 (D) S1和S5 答复: (B) 说明: FORTRAN的按引用传递特性使其能够传递指针:

  1. 将单元格绑定到其子例程(如果它们已经存在)
  2. 如果不存在未命名的临时单元格,则稍后将其绑定

现在,让我们分析以下陈述:

(S1) Because the second argument is an expression, which could be evaluated 
easily by compiler SDT rules to a value 13, compiler itself will generate code 
to define and declare an unnamed temporary cell with value 13 and pass it to swap 
subroutine. [CORRECT]

(S2) 'ix' and 'iy' are variables bound to valid mutable cells, thus there is no 
reason to get a run time error on line L1. [INCORRECT]

(S3) Incorrect due to same reason as of S2 [INCORRECT]

(S4) Due to the pass-by-reference nature of the language, the cell bound to 
variable 'ia' will get value 13 and the temporary unnamed cell which was allocated 
and passed to the swap subroutine will get value 3. Seemingly, cell bound to variable
 'ib' is unchanged, thus printing 13 and 8 at the end of this routine. [CORRECT]

(S5) Incorrect due to same reason as of S4 [INCORRECT]

因此,答案应该是(B)S1和S4

这一解释是由 维内特·珀斯瓦尼。 这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享