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

📅  最后修改于: 2021-09-26 03:37:38             🧑  作者: 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,并传递单元格的地址swap
S2:执行时,代码将在 L1 行生成运行时错误
S3:执行时,代码将在 L2 行生成运行时错误
S4:程序将打印 13 和 8
S5:程序将打印 13 和 -2
以下一组陈述是正确的:
(A) S1 和 S2
(B) S1 和 S4
(C) S3
(D) S1 和 S5答案:(乙)
解释:
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 提供。
这个问题的测验