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 output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(const int &a)
{
int temp = 10;
a = temp;
cout<< a;
}
int main(int argc, char const *argv[])
{
int a = 5;
func(a);
return 0;
}

What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(const int &a)
{
int temp = 10;
a = temp;
cout<< a;
}
int main(int argc, char const *argv[])
{
int a = 5;
func(a);
return 0;
}
a) 5
b) 10
c) error
d) Segmentation fault

Answer: c
Explanation: As we are passing a as const reference to function therefore its value cannot be changes inside the function. So the program gives error.

Join The Discussion