edited by
2,078 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

2.6k
views
3 answers
12 votes
Arjun asked Dec 18, 2018
2,598 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(“x”); } for(int i=0;i<=n-1;++i){ printx(n-1); } }$625$256$120$24$5$
3.8k
views
2 answers
12 votes
Arjun asked Dec 18, 2018
3,758 views
Consider the following program fragment:var x, y: integer; x := 1; y := 0; while y < x do begin x := 2*x; y := y+1 end;For the above fragment , which of the ... $x=2^y$None of the above, since the loop does not terminate
1.8k
views
2 answers
9 votes
makhdoom ghaya asked Oct 22, 2015
1,774 views
Various parameter passing mechanisms have been in used in different programming languages. Which of the following statements is true?Call by value result is used in language ... the same as call by name.Call by name is the most efficient.
1.3k
views
1 answers
1 votes
Arjun asked Dec 18, 2018
1,266 views
Consider the following toy model of traffic on a straight , single lane, highway. We think of cars as points, which move at the maximum speed $v$ that satisfies ... relationship between the speed $v$ and the density $\rho$ in this model ?