C++ Programming Questions and Answers – Value Return

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. 3
Explanation 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. void
Explanation 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 function
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.

Q 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

Show Answer Answer:-B. 7
Explanation In this program, we are returning the maximum value by using conditional operator. Output: $ g++ ret.cpp $ a.out 7

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

  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

Show Answer Answer:-A. 46.5
Explanation We are returning the value what we get as input. Output: $ g++ ret1.cpp $ a.out 46.5

Q 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

Show Answer Answer:-B. 25
Explanation We are multiplying these values by adding every values. Output: $ g++ ret.cpp $ a.out 25

Q 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 arguments
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.

Q 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. 5
B. 25
C. 375
D. 565

Show Answer Answer:-A. 5
Explanation In this program, we are finding the gcd of the number. Output: $ g++ ret5.cpp $ a.out 5

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page