• edited by
20,270 views
35 35 votes

Consider the following function

void swap(int a, int b)  
{       
    int temp;
    temp = a;
    a = b;
    b = temp;  
}  

In order to exchange the values of two variables $x$ and $y$.

  1. call $swap(x, y)$

  2. call $swap(\&x, \&y)$

  3. $swap (x, y)$ cannot be used as it does not return any value

  4. $swap (x, y)$ cannot be used as the parameters are passed by value

5 Answers

Best answer
45 45 votes

ans (D).

Option A will not exchange the values of $x$ and $y$ because parameters are passed by value in C. i.e., the code is exchanging $x'$ and $y'$ which are having the values of $x$ and $y$ respectively. 

Option B will not swap the value

 void swap(int a, int b) 

Here, it is wrong to pass in address (int*) as the parameters are of int type, even sizeof int and int* varies depending on the compiler. Now, even if ignoring this error, the given code would not exchange the values of $x$ and $y$ as it is merely exchanging $p_1'$ and $p_2'$ where $p_1'$ and $p_2'$ are containing the copies of the addresses of $x$ and $y$ respectively. (Even addresses are passed by value in C language).

Option C is false, return value is not required for exchanging the variables.

Option D is correct. We cannot use $swap(x,y)$ because parameters are passed by value. Only way now to exchange the variables are by passing their addresses and then modifying the contents using the de-referencing operator $(*).$

• edited by
11 11 votes
I think, option D will be answer.

Because in the given function, parameters are used as passed by value.So there will be no change in actual parameters which is defined under the main function..
1 1 vote
Correct answer: Option D, swap(x,y)swap(x,y) cannot be used as the parameters are passed by value.

a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value.
b) call swap (&x, &y) will no work as function swap() expects values, not addresses (or pointers).
c) swap (x, y) cannot be used but the reason given is not correct.
0 0 votes

Pass by value: In this approach, we pass the copy of actual variables in function as a parameter. Hence any modification on parameters inside the function will not reflect in the actual variable.

Therefore to exchange the values of x and y, we cannot use pass by value( given code is for pass by value). So option D is correct

Answer:
Position:
Show:

Related questions

9 9 votes
5 answers 5 answers
9.0k
9.0k views
go_editor asked Sep 28, 2014
8,956 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
16 16 votes
7 answers 7 answers
15.1k
15.1k views
Kathleen asked Sep 18, 2014
15,101 views
The problem $\text{3-SAT}$ and $\text{2-SAT}$ are both in $\text{P}$both $\text{NP}$ complete$\text{NP}$-complete and in $\text{P}$ respectivelyundecidable and $\text{NP}...
15 15 votes
7 7 answers
11.0k
11.0k views
Kathleen asked Oct 8, 2014
11,004 views
What is the value of $X$ printed by the following program?program COMPUTE (input, output); var X:integer; procedure FIND (X:real); begin X:=sqrt(X); end; begin X:=2 FIND(...
61 61 votes
8 answers 8 answers
28.8k
28.8k views
Kathleen asked Sep 22, 2014
28,776 views
What does the following C-statement declare?int (*f) (int * );A function that takes an integer pointer as argument and returns an integerA function that takes an integer ...