250+ TOP MCQs on Exceptions That Are Not Errors and Answers

This section on C++ MCQs (multiple choice questions) on “Exceptions That Are Not Errors”. One shall practice these MCQs 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 multiple choice questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C multiple choice questions on “Exceptions That Are Not Errors” along with answers, explanations and/or solutions:

1. Which is used to handle the exceptions in c++?
a) catch handler
b) handler
c) exception handler
d) throw
Answer: c
Clarification: Exception handler is used to handle the exceptions in c++.

2. Which type of program is recommended to include in try block?
a) static memory allocation
b) dynamic memory allocation
c) const reference
d) pointer
Answer: b
Clarification: While during dynamic memory allocation, Your system may not have sufficient resources to handle it, So it is better to use it inside the try block.

3. Which statement is used to catch all types of exceptions?
a) catch()
b) catch(Test t)
c) catch(…)
d) catch(Test)
Answer: c
Clarification: This catch statement will catch all types of exceptions that arises in the program.

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

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x = -1;
  6.         try 
  7.         {
  8.             if (x < 0)
  9.             {
  10.                 throw x;
  11.             }
  12.             else
  13.             {
  14.                 cout<<x;
  15.             }
  16.         }
  17.         catch (int x )
  18.         {
  19.             cout << "Exception occurred: Thrown value is " << x << endl;
  20.         }
  21.         return 0;
  22.     }

a) -1
b) 0
c) Exception occurred: Thrown value is -1
d) Error
Answer: c
Clarification: As the given value is -1 and according to the condition, We are arising an exception.
Output:

$ g++ etae.cpp
$ a.out
Exception occurred: Thrown value is -1

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class Polymorphic {virtual void Member(){}};
  5.     int main () 
  6.     {
  7.         try
  8.         {
  9.             Polymorphic * pb = 0;
  10.             typeid(*pb);   
  11.         }
  12.         catch (exception& e)
  13.         {
  14.             cerr << "exception caught: " << e.what() << endl;
  15.         }
  16.         return 0;
  17.     }

a) exception caught: std::bad_typeid
b) exception caught: std::bad_alloc
c) exception caught: std::bad_cast
d) exception caught: std::bad_id
Answer: a
Clarification: In this program, We used a bad type id for the polymorphic operator, So it is arising an bad_typeid exception.
Output:

$ g++ etae.cpp
$ a.out
exception caught: std::bad_typeid

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     void myunexpected () 
  5.     {
  6.         cout << "unexpected handler calledn";
  7.         throw;
  8.     }
  9.     void myfunction () throw (int,bad_exception) 
  10.     {
  11.         throw 'x';
  12.     }
  13.     int main (void)
  14.     {
  15.         set_unexpected (myunexpected);
  16.         try 
  17.         {
  18.             myfunction();
  19.         }    
  20.         catch (int) 
  21.         { 
  22.             cout << "caught intn"; 
  23.         }
  24.         catch (bad_exception be) 
  25.         { 
  26.             cout << "caught bad_exceptionn"; 
  27.         }
  28.         catch (...) 
  29.         { 
  30.             cout << "caught other exception n"; 
  31.         }
  32.         return 0;
  33.     }

a) unexpected handler called
b) caught bad_exception
c) caught other exception
d) both unexpected handler called & caught bad_exception
Answer: d
Clarification: In this program, We are calling set_unexpected and myfunction, So it is printing the output as the given.
Output:

$ g++ etae.cpp
$ a.out
unexpected handler called
caught bad_exception

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

  1.     #include 
  2.     using namespace std; 
  3.     int main()
  4.     {
  5.         int x = -1;
  6.         char *ptr;
  7.         ptr = new char[256];
  8.         try 
  9.         {
  10.             if (x < 0)
  11.             {
  12.                 throw x;
  13.             }
  14.             if (ptr == NULL)
  15.             {
  16.                 throw " ptr is NULL ";
  17.             }
  18.         }
  19.         catch (...) 
  20.         {
  21.             cout << "Exception occurred: exiting "<< endl;
  22.         }
  23.         return 0;
  24.     }

a) -1
b) ptr is NULL
c) exception occured: exiting
d) 1
Answer: c
Clarification: catch(…) is used to catch all types of exceptions arising in the program.
Output:

$ g++ etea.cpp
$ a.out
Exception occured: exiting

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     void myunexpected ()
  5.     {
  6.         cout << "unexpected calledn";
  7.         throw 0;
  8.     }
  9.     void myfunction () throw (int) 
  10.     {
  11.         throw 'x';
  12.     }
  13.     int main () 
  14.     {
  15.         set_unexpected (myunexpected);
  16.         try 
  17.         {
  18.             myfunction();
  19.         }
  20.         catch (int) 
  21.         {
  22.             cout << "caught intn";
  23.         }
  24.         catch (...)  
  25.         { 
  26.             cout << "caught other exceptionn"; 
  27.         }
  28.         return 0;
  29.     }

a) caught other exception
b) caught int
c) unexpected called
d) both caught int & unexpected called
Answer: d
Clarification: As we are calling set_unexpected (myunexpected) function, this is printing as unexpected called and because of operator compliance it is arising an exception.
Output:

$ g++ etea.cpp
$ a.out
unexpected called
caught int

9. How to handle error in the destructor?
a) throwing
b) terminate
c) both throwing & terminate
d) try
Answer: b
Clarification: It will not throw an exception from the destructor but it will the process by using terminate() function.

10. What kind of exceptions are available in c++?
a) handled
b) unhandled
c) static
d) dynamic
Answer: b
Clarification: unhandled kind of exceptions are available in c++.

  250+ TOP MCQs on Unspecified Number of Arguments and Answers

Leave a Comment

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

Scroll to Top