in Object Oriented Programming edited by
4,640 views
1 vote
1 vote

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
in Object Oriented Programming edited by
4.6k views

1 comment

c only..!
0
0

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 vote
1 vote
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 vote
1 vote
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

2 Comments

edited by

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

Official answer key
Caption

 

1
1
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
0
0
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

2 Comments

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
0
0
ok..thnks for the correction
0
0

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true