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.
Related Posts
When we define the default values for a function?
In which of the following cases inline functions may not word?
i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursiveAn inline function is expanded during ______________
What is an inline function?
What happens to a function defined inside a class without any complex operations (like looping, a large number of lines, etc)?
Which of the following is the default return value of functions in C++?
How many minimum number of functions should be present in a C++ program for its execution?
Join The Discussion