All mock tests are FREE till 31 December — register to save your progress
Unit 3 · Programming Languages and Computer Graphics Paper 2 CS · June 2024 (Re-Exam)

#include<iostream.h>
using namespace std;
void swap(int &x, int &y)
{
int temp=x;
x=y;
y=temp;
}
int main()
{
int a=5, b=10;
swap(a,b);
swap(a,b);
cout<<a<<" "<<b;
return 0;
}

What will be the output of the above code?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: A. 5, 10

Explanation

swap takes references, so the first call makes a = 10, b = 5 and the second swaps them back. Output: 5 10.

More Programming Languages and Computer Graphics questions