This section on C++ interview questions and answers on “Value Return”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
-
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
Answer: b
Clarification: In this program, we are returning the maximum value by using conditional operator.
Output:
5. What will be the output of the following C++ code?
-
#include
-
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
Answer: a
Clarification: 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
-
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
Answer: b
Clarification: We are multiplying these values by adding every values.
Output:
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
Answer: a
Clarification: 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
-
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
Answer: d
Clarification: In this program, we are finding the gcd of the number.
Output: