3,726 views

Given below are three implementations of the $\text{swap()}$ function in $\text{C++}$ :

$\begin{array}{|l|l|l|} \hline \qquad \qquad \text{(a)} & \qquad \qquad \text{(b)} & \qquad \qquad \text{(c)} \\ \text{void swap (int a, int b)} & \text{void swap (int &a, int &b)} & \text{void swap (int *a, int *b)} \\ \text{\{} & \text{\{} & \text{\{} \\ \text{int temp;} & \text{int temp;} & \text{int *temp;} \\ \text{temp = a;} & \text{temp = a;} & \text{temp = a;} \\ \text{a = b;} & \text{a = b;} & \text{a = b;} \\ \text{b = temp;} & \text{b = temp;} & \text{b = temp;} \\ \text{\}} & \text{\}} & \text{\}} \\ \text{int main()} & \text{int main()} & \text{int main()} \\ \text{int p = 0,q = 1;} & \text{int p = 0, int q = 1;} & \text{int p = 0 , q = 1;} \\ \text{swap(p,q);} & \text{swap(p,q);} &\text{swap(p,q);} \\ \text{\}} & \text{\}} & \text{\}}\\\hline \end{array}$

Which of these would actually swap the contents of the two integer variables $\text{p}$ and $\text{q}$?

1. $\text{(a)}$ only
2. $\text{(b)}$ only
3. $\text{(c)}$ only
4. $\text{(b)}$ and $\text{(c)}$ only

### 1 comment

c only..!

It is not asked for c, it is asked for c++ and in c++ &a=b implies somewhat like pointer thing but not pointer. Hence B is correct option.moreover option c is incorrect as it is swapping pointers not the values.
option A:- swap is takes place in local variables ===> does not imply swap of p and q

Option C :- pointers are swapped but not the values which are pointed by pointers ===> does not imply swap of p and q

Option B is using Passed By Reference ===> imply swap of p and q
Only C.

A performs the swap on the local variables - the original variables are not changed.

B is incorrect declaration.

C is the only one which swaps correctly.
by

edited by

I also answered 'C' but official key show 'B' is the correct...
Can you explain ?

I don't think so that option (b) is incorrect declaration
Rather part (c) is incorrect because inside swap function definition there is incorrect assignment of values at the addresses of a and b

Thus answer should be option 2 i.e (b) only

If I am wrong then please correct me

(a) first of all execution will come to main(),here p=0 and q=1

now swap(p ,q) will be executed so value of p and q will be copied to a and b so now a=0 and b=1.

here a temporary variable is declared ,temp=a =>temp=0 ,a=b=>a=1 and b=temp=>b=0

but here actual parameters are not swapped ,formal parameters are swapped .(call by value)

(b) here p=0 , q=1 now swap(p,q) is called and  here &a and &b is used so &a and &b will be stored in p and q now swapping will be done in actual parameters.

(c) here p=0,q=1 now swap(&p,&q) is called and a and p are pointer  which will store the address of p and q respectively

now a temporary pointer is declared and temp =a means temp will store the address of  a ,a=b means a will store the address of b ,b=temp means b will store the address of temp but here value of p and q is not swapped.

so  option( 2) is correct

by

in option b we are using reference variable a and b which are assigned  values p and q and when a and b exchanges the effect appears in p and q as reference variables are used
ok..thnks for the correction