Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What will be the new value of x in the following C++ code?
#include <iostream>
using namespace std;
void fun(int &x)
{
x = 20;
}
int main()
{
int x = 10;
fun(x);
cout << “New value of x is ” << x;
return 0;
}

What will be the new value of x in the following C++ code?
#include <iostream>
using namespace std;
void fun(int &x)
{
x = 20;
}
int main()
{
int x = 10;
fun(x);
cout << "New value of x is " << x;
return 0;
}
a) 10
b) 20
c) 15
d) 36

Answer: b
Explanation: As the parameter is passed by reference, the value in the original memory of x is changed hence the output is printed as 20.
Output:
20

Join The Discussion