5) void swap(int &, int &); void main() { int a=10, b=20; swap(a++,b++); printf(“\n%d\t%d\t”,a,b); } void swap(int &x, int &y) { x+=2; y+=3; }
Reveal answer
Fill a bubble to check yourself
5) void swap(int &, int &); void main() { int a=10, b=20; swap(a++,b++); printf(“\n%d\t%d\t”,a,b); } void swap(int &x, int &y) { x+=2; y+=3; }
11 21
13 24
10 20
12 23
To answer this question, we need to understand how the function swap works and what happens when it is called.
The function swap takes two integer references as parameters. It adds 2 to the first parameter and 3 to the second parameter.
In the given code, the main function initializes two variables a and b with values 10 and 20 respectively. Then, the swap function is called with the expressions a++ and b++ as arguments.
When the expression a++ is passed as an argument, the value of a is first used in the swap function and then incremented by 1. Similarly, the value of b is used and then incremented by 1.
Therefore, inside the swap function, x is 10 and y is 20. After adding 2 and 3 to x and y respectively, x becomes 12 and y becomes 23.
Finally, in the printf statement, the values of a and b are printed. Since a++ was passed as an argument to swap, a was incremented by 1 before being used in the printf statement. Therefore, the value of a is 11. The value of b is 21 because it was incremented by 1 in the same way.
Hence, the correct answer is:
Option A) 11 21