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
- What is the output of the code given below?#include<iostream> using namespace std; class Base { public: Base() { cout << "Base Const"; } vi…
- Consider the following C code:#include<stdio.h> int temp = 0; int fun(int x, int y) { int z; temp++; if (y == 3) return (x*x*x); else { z =…
- Match List I with List IIList I List II (A) HTML (I) Allows for dynamic and interactive web pages. (B) DHTML (II) Defines the structure of…
- Arrange the following steps of file handling in C in the correct order: (A) Close the file (B) Read from or write to the file (C) Open the…
- What will be the output of the following C code?#include<stdio.h> void main() { int arr[5] = {10,20,30,40,50}; int *p = (int*)(&arr + 1); p…
- Find the output of the following code:#include<stdio.h> void main() { int arr[] = {1, 2, 3, 4, 5}; int *p = arr; printf("%d", *p++); printf…