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
(D) S1 and S5
Pass-by-reference nature of FORTRAN makes it pass pointers:
Attention reader! Don’t stop learning now. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.
- to bound cells to its subroutines, if they are already present
- 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