What will be the output of the following C++ code?
void change(int &x)
{
x = 50;
}
int main()
{
int a = 10;
change(a);
cout << a;
}
Options
-
A.
10
-
B.
40
-
C.
50
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
C.
50
Detailed Explanation
Answer: (C) 50
The parameter int &x is a reference to the original variable passed to the function.
When change(a) is called, x refers directly to a.
Therefore, x = 50 changes the original variable a to 50.
Hence, the output is 50.