All mock tests are FREE till 31 December — register to save your progress

Consider the following C++ program:
#include<iostream>
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 each call really swaps a and b. Swapping twice restores the original values: 5 10.

More Programming Languages and Computer Graphics questions