C++ interview questions and answers focuses on “Value Return”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews entrance exams and other competitive exams.
C++ interview questions on “Value Return” along with answers, explanations and/or solutions:
Q 1. How many types of returning values are present in c++?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-C. 3Explanation
The three types of returning values are return by value, return by reference and return by address.Q 2. What will you use if you are not intended to get a return value?
A. static
B. const
C. volatile
D. void
Show Answer
Answer:-D. voidExplanation
Void is used to not to return anything.Q 3. Where does the return statement returns the execution of the program?
A. main function
B. block function
C. same function
D. caller function
View Answer
Show Answer
Answer:-D. caller functionExplanation
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.Q 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
Show Answer
Answer:-B. 7Explanation
In this program, we are returning the maximum value by using conditional operator. Output: $ g++ ret.cpp $ a.out 7Q 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
Show Answer
Answer:-A. 46.5Explanation
We are returning the value what we get as input. Output: $ g++ ret1.cpp $ a.out 46.5Q 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
Show Answer
Answer:-B. 25Explanation
We are multiplying these values by adding every values. Output: $ g++ ret.cpp $ a.out 25Q 7. When will we use the function overloading?
A. same function name but same number of arguments
B. different function name but same number of arguments
C. same function name but different number of arguments
D. different function name but different number of arguments
Show Answer
Answer:-C. same function name but different number of argumentsExplanation
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.Q 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. 5
B. 25
C. 375
D. 565
Leave a Reply