edited by
4,595 views
1 votes
1 votes

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
edited by

6 Answers

3 votes
3 votes
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.
1 votes
1 votes
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
1 votes
1 votes
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.
0 votes
0 votes

(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

 

edited by

Related questions

1 votes
1 votes
2 answers
2
Pooja Khatri asked Jul 13, 2018
2,655 views
The definitions in an XML document are said to be ______ when the tagging system ans definitions in the DTD are all in compliancewell-formedreasonablevalidlogical