📜  门| GATE-CS-2006 |第56章

📅  最后修改于: 2021-06-28 20:41:12             🧑  作者: Mango

请考虑以下以传递引用语言(例如FORTRAN)编写的代码,以及有关该代码的这些语句。

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

该解释由Vineet Purswani提供。
这个问题的测验