Consider the following code written in a pass-by-reference language like…

2009

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, ib+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 to 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:

  1. A.

    S1 and S2

  2. B.

    S1 and S4

  3. C.

    S3

  4. D.

    S1 and S5

Attempted by 68 students.

Show answer & explanation

Correct answer: B

In FORTRAN, arguments are passed by reference. When an expression like ib+5 is passed as an argument, the compiler creates a temporary variable initialized to 13. Thus, statement S1 is correct. Inside the subroutine, ix refers to ia and iy refers to this temporary variable. The swap operation sets ia (via ix) to 13 and the temporary variable to 3. Since iy points to the temporary, not ib, ib remains unchanged at 8. Therefore, the program prints 13 and 8, making S4 correct.

Explore the full course: Isro