Here is a listing of C++ interview questions on “Value Return” along with answers, explanations and/or solutions:
1. How many types of returning values are present in c++?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: The three types of returning values are return by value, return by reference and return by address.
2. What will you use if you are not intended to get a return value?
a) static
b) const
c) volatile
d) void
View Answer
Explanation: Void is used to not to return anything.
3. Where does the return statement returns the execution of the program?
a) main function
b) caller function
c) same function
d) block function
View Answer
Explanation: The execution of the program is returned to the point from where the function was called and the function from which this function was called is known as caller function.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int max(int a, int b )
{
return ( a > b ? a : b );
}
int main()
{
int i = 5;
int j = 7;
cout << max(i, j );
return 0;
}
a) 5
b) 7
c) either 5 or 7
d) 13
View Answer
Explanation: In this program, we are returning the maximum value by using conditional operator.
Output:
$ g++ ret.cpp $ a.out 7
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
double & WeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
int main()
{
double hours = WeeklyHours();
cout << "Weekly Hours: " << hours;
return 0;
}
a) 46.5
b) 6.50
c) compile time error
d) 26.5
View Answer
Explanation: We are returning the value what we get as input.
Output:
$ g++ ret1.cpp $ a.out 46.5
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int mult (int x, int y)
{
int result;
result = 0;
while (y != 0)
{
result = result + x;
y = y - 1;
}
return(result);
}
int main ()
{
int x = 5, y = 5;
cout << mult(x, y) ;
return(0);
}
a) 20
b) 25
c) 30
d) 35
View Answer
Explanation: We are multiplying these values by adding every values.
Output:
$ g++ ret.cpp $ a.out 25
7. When will we use the function overloading?
a) same function name but different number of arguments
b) different function name but same number of arguments
c) same function name but same number of arguments
d) different function name but different number of arguments
View Answer
Explanation: We use function overloading when we want the same name function to perform different procedure for different types of parameters or different number of parameters provided to the function.
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int gcd (int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return(a);
}
int main ()
{
int x = 15, y = 25;
cout << gcd(x, y);
return(0);
}
a) 15
b) 25
c) 375
d) 5
View Answer
Explanation: In this program, we are finding the gcd of the number.
Output:
$ g++ ret5.cpp $ a.out 5
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for Computer Science Internship
- Check Programming Books