GATE | GATE-CS-2006 | Question 56

Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.

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: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap
S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and -2
Exactly the following set of statement(s) is correct:
(A) S1 and S2
(B) S1 and S4
(C) S3
(D) S1 and S5


Answer: (B)

Explanation:  
Pass-by-reference nature of FORTRAN makes it pass pointers:

  1. to bound cells to its subroutines, if they are already present
  2. to unnamed temporary cells later to be bound, if they are not there

Now let’s analyse the given statements:



(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]

Hence, answer should be (B) S1 and S4

This explanation has been contributed by Vineet Purswani.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.