retagged by
6,666 views
15 votes
15 votes

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. S1 and S2 
  2. S1 and S4 
  3. S3 
  4. S1 and S5 
retagged by

2 Answers

Best answer
16 votes
16 votes
S1 and S4 are correct. There won't be any runtime error for the given code using pass by reference.
selected by
12 votes
12 votes
    call swap (ia, ib+5)

The first parameter is passed by reference. For the second parameter, ib isn't passed by reference; rather a temporary memory cell is passed, whose value if 8+5=13. Let's name it p.

p swaps with a, hence a gets the value 13; p gets the value 3. We'll never refer to p again now.

 

    print *, ia, ib 

This would print 13, 8 because ib was never passed by reference, its value stays intact.

 

$S_1$ and $S_4$ are correct



Line 1 and Line 2 won't generate a runtime error, as ints are being assigned to ints. So, $S_2$ and $S_3$ are incorrect.

And since $S_4$ is correct, $S_5$ is incorrect.

Answer:

Related questions

30 votes
30 votes
3 answers
2
gatecse asked Sep 15, 2014
11,297 views
Consider the polynomial $p(x) = a_0 + a_1x + a_2x^2 + a_3x^3$ , where $a_i \neq 0$, $\forall i$. The minimum number of multiplications needed to evaluate $p$ on an input ...