edited by
1,956 views
5 votes
5 votes

Consider the following program fragment:

var a,b : integer;
procedure G(c,d: integer);
begin
    c:=c-d;
    d:=c+d;
    c:=d-c
end;
a:=2;
b:=3;
G(a,b);

If both parameters to $G$ are passed by reference, what are the values of $a$ and $b$ at the end of the above program fragment ?

  1. $a=0$ and $b=2$
  2. $a=3$ and $b=2$
  3. $a=2$ and $b=3$
  4. $a=1$ and $b=5$
  5. None of the above
edited by

2 Answers

Best answer
12 votes
12 votes
Option B is correct as for the given inputs the program fragments does swapping of the values. Call-by-reference implies that the swapped values are reflected in the calling function as well.

PS: Procedure $G$ is not doing proper swapping for all input values as for negative values, there is a chance of underflow in $c - d.$
Answer:

Related questions

12 votes
12 votes
3 answers
1
Arjun asked Dec 18, 2018
2,395 views
Given the following pseudocode for function $\text{printx()}$ below, how many times is $x$ printed if we execute $\text{printx(5)}?$void printx(int n) { if(n==0){ printf...
12 votes
12 votes
2 answers
2
1 votes
1 votes
1 answer
4