250+ TOP MCQs on Error Handling Alternatives and Answers

This section on C++ programming interview questions and answers 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
Answer: c
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: Set a global error indicator is most suitable for returning the logical errors in the program.

  250+ TOP MCQs on STL – Pair and Answers

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

  1.     #include 
  2.     #include 
  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

b) x
c) Both c & x
d) c
Answer: a
Clarification: We are checking the type id of char and float as they are not equal, We are printing c.
Output:

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

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

  1.     #include 
  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
Answer: a
Clarification: We are dividing 0 and 10 in this program and we are using the throw statement in the function block.
Output:

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

  1.     #include 
  2.     #include 
  3.     #include 
  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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: As in the case of not using an exception, it will remain useless in the program and increase the code complexity.

Leave a Comment

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

Scroll to Top