C++ Programming Questions and Answers – Value Return

This section on 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 (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
View Answer

Answer: c
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

Answer: d
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

Answer: b
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.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int max(int a, int b )
  4.     {
  5.         return ( a > b ? a : b );
  6.     }
  7.     int main()
  8.     {
  9.         int i = 5;
  10.         int j = 7;
  11.         cout << max(i, j );
  12.         return 0;
  13.     }

a) 5
b) 7
c) either 5 or 7
d) 13
View Answer

Answer: b
Explanation: In this program, we are returning the maximum value by using conditional operator.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ ret.cpp
$ a.out
7

advertisement

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     double & WeeklyHours()
  4.     {
  5.         double h = 46.50;
  6.         double &hours = h;
  7.         return hours;
  8.     }
  9.     int main()
  10.     {
  11.         double hours = WeeklyHours();
  12.         cout << "Weekly Hours: " << hours;
  13.         return 0;
  14.     }

a) 46.5
b) 6.50
c) compile time error
d) 26.5
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int mult (int x, int y)
  4.     {
  5.         int result;
  6.         result = 0;
  7.         while (y != 0) 
  8.         {
  9.             result = result + x;
  10.             y = y - 1;
  11.         }
  12.         return(result);
  13.     }
  14.     int main ()
  15.     {
  16.         int x = 5, y = 5;
  17.         cout  << mult(x, y) ;
  18.         return(0);
  19.     }

a) 20
b) 25
c) 30
d) 35
View Answer

Answer: b
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

Answer: a
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?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int gcd (int a, int b)
  4.     {
  5.         int temp;
  6.         while (b != 0) 
  7.         {
  8.             temp = a % b;
  9.             a = b;
  10.             b = temp;
  11.         }
  12.         return(a);
  13.     }
  14.     int main ()
  15.     {
  16.         int x = 15, y = 25;
  17.         cout << gcd(x, y);
  18.         return(0);
  19.     }

a) 15
b) 25
c) 375
d) 5
View Answer

Answer: d
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.