C++ Programming Questions and Answers – Error Handling Alternatives

This section on C++ programming interview questions and answers focuses on “Error Handling Alternatives”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in 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++ programming interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ programming interview questions on “Error Handling Alternatives” along with answers, explanations and/or solutions:

1. Which alternative can replace the throw statement?
a) for
b) break
c) return
d) exit
View Answer

Answer: c
Explanation: throw and return does the same job as return a value. So it can be replaced.

2. What are the disadvantages if use return keyword to return error codes?
a) You have to handle all exceptional cases explicitly
b) Your code size increases dramatically
c) The code becomes more difficult to read
d) All of the mentioned
View Answer

Answer: d
Explanation: As we are using return for each and every exception, It will definitely increase the code size.

3. What is most suitable for returning the logical errors in the program?
a) Use constructor and destructor
b) Set a global error indicator
c) Use break keyword
d) Use final keyword
View Answer

Answer: b
Explanation: Set a global error indicator is most suitable for returning the logical errors in the program.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <typeinfo>
  3.     using namespace std;
  4.     class A 
  5.     { 
  6.     };
  7.     int main()
  8.     { 
  9.         char c; float x;
  10.         if (typeid(c) != typeid(x))
  11.         cout << typeid(c).name() << endl;
  12.         cout << typeid(A).name();
  13.         return 0;
  14.     }

a)

c
1A
Note: Join free Sanfoundry classes at Telegram or Youtube

b) x
c) Both c & x
d) c
View Answer

Answer: a
Explanation: We are checking the type id of char and float as they are not equal, We are printing c.
Output:

advertisement
$ g++ eal.cpp
$ a.out
c
1A

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     void Division(const double a, const double b);
  4.     int main()
  5.     {
  6.         double op1=0, op2=10;
  7.         try 
  8.         {
  9.             Division(op1, op2);
  10.         }
  11.         catch (const char* Str)
  12.         {
  13.             cout << "\nBad Operator: " << Str;
  14.         }
  15.         return 0;
  16.     }
  17.     void Division(const double a, const double b)
  18.     {
  19.         double res;
  20.         if (b == 0)
  21.             throw "Division by zero not allowed";
  22.         res = a / b;
  23.         cout << res;
  24.     }

a) 0
b) Bad operator
c) 10
d) 15
View Answer

Answer: a
Explanation: We are dividing 0 and 10 in this program and we are using the throw statement in the function block.
Output:

$ g++ eal.cpp
$ a.out
0

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

  1.     #include <stdexcept>
  2.     #include <limits>
  3.     #include <iostream>
  4.     using namespace std;
  5.     void MyFunc(char c)
  6.     {
  7.         if (c < numeric_limits<char>::max())
  8.             return invalid_argument;
  9.     }
  10.     int main()
  11.     {
  12.         try
  13.         {
  14.             MyFunc(256);
  15.         }
  16.         catch(invalid_argument& e)
  17.         {
  18.             cerr << e.what() << endl;
  19.             return -1;
  20.         }
  21.         return 0;
  22.     }

a) 256
b) Invalid argument
c) Error
d) 246
View Answer

Answer: c
Explanation: We can’t return a statement by using the return keyword, So it is arising an error.

7. What is the use of RAII in c++ programming?
a) Improve the exception safety
b) Terminate the program
c) Exit from the block
d) Crash the compiler
View Answer

Answer: a
Explanation: RAII is used to improve the exception safety.

8. How many levels are there in exception safety?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: The three levels of exception safety are basic, strong and no throw.

9. Pick out the correct statement for error handling alternatives.
a) Terminate the program
b) Use the stack
c) Exit from the block
d) Use the queue
View Answer

Answer: b
Explanation: When an error is raised means, it will be pushed into stack and it can be corrected later by the programmer.

10. What will happen when an exception is not processed?
a) It will eat up a lot of memory and program size
b) Terminate the program
c) Crash the compiler
d) Displays proper output
View Answer

Answer: a
Explanation: As in the case of not using an exception, it will remain useless in the program and increase the code complexity.

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.