250+ TOP MCQs on Uncaught Exceptions and Answers

This section on C++ programming questions and answers on “Uncaught Exceptions”. One shall practice these 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 questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ programming questions on “Uncaught Exceptions” along with answers, explanations and/or solutions:

1. What happens if try catch block is not used?
a) arise an error
b) program will run
c) execute continuously
d) wrong output
Answer: a
Clarification: If try catch block is not used the exception thrown by the program will be uncaught hence will result into error(s).

2. Which handler is used to handle all types of exception?
a) catch handler
b) catch-all handler
c) catch-none handler
d) try-catch handler
Answer: b
Clarification: To catch all types of exceptions, we use the catch-all handler.

3. Which operator is used as catch-all handler?
a) ellipses operator
b) ternary operator
c) string operator
d) unary operator
Answer: a
Clarification: The ellipses operator can be represented as (…).

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

  1.     #include 
  2.     using namespace std;
  3.     class Base
  4.     {
  5.         protected:
  6.         int a;
  7.         public:
  8.         Base() 
  9.         { 
  10.             a = 34; 
  11.         }
  12.         Base(int i)
  13.         { 
  14.             a = i; 
  15.         }
  16.         virtual ~Base() 
  17.         { 
  18.             if (a < 0)  throw a; 
  19.         }
  20.         virtual int getA()
  21.         {
  22.             if (a < 0) 
  23.             { 
  24.                 throw a;
  25.             }
  26.         }
  27.     };
  28.     int main()
  29.     {
  30.         try
  31.         {
  32.             Base b(-25);
  33.             cout << endl << b.getA();
  34.         }
  35.         catch (int) 
  36.         {
  37.             cout << endl << "Illegal initialization";
  38.         }
  39.     }

a) Illegal initialization
b) Terminate called after throwing an instance of ‘int’
c) Illegal initialization & terminate called after throwing an instance
d) initialization
Answer: b
Clarification: As we are throwing a negative number and we are using the only integer, So it is arising an error.
Output:

$ g++ uce.cpp
$ a.out
terminate called after throwing an instance of 'int'

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     void terminator()
  5.     {
  6.         cout << "terminate" << endl;
  7.     }
  8.     void (*old_terminate)() = set_terminate(terminator);
  9.     class Botch 
  10.     {
  11.         public:
  12.         class Fruit {};
  13.         void f() 
  14.         {
  15.             cout << "one" << endl;
  16.             throw Fruit();
  17.         }
  18.         ~Botch() 
  19.         {
  20.             throw 'c'; 
  21.         }
  22.     };
  23.     int main() 
  24.     {
  25.         try 
  26.         {
  27.             Botch b;
  28.             b.f();
  29.         } 
  30.         catch(...) 
  31.         {
  32.             cout << "inside catch(...)" << endl;
  33.         }
  34.     }

a) one
b) inside catch
c)

   one 
   terminate

d)

   one 
   terminate
   Aborted

View Answer

Answer: d
Clarification: This program uses set_terminate as it is having an uncaught exception.
Output:

$ g++ uce1.cpp
$ a.out
one 
terminate
Aborted

 
 

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     void myterminate () 
  6.     {
  7.         cerr << "terminate handler called";
  8.         abort();
  9.     }
  10.     int main (void) 
  11.     {
  12.         set_terminate (myterminate);
  13.         throw 0; 
  14.         return 0;
  15.     }

a) terminate handler called
b) aborted
c) both terminate handler & Aborted
d) runtime error
Answer: c
Clarification: In this program, We are using set_terminate to abort the program.
Output:

$ g++ uce2.cpp
$ a.out
terminate handler called
Aborted

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

  1.     #include 
  2.     using namespace std;
  3.     class Test1 
  4.     { 
  5.     };
  6.     class Test2 : public Test1 { };
  7.     void Funct();
  8.     int main()
  9.     {
  10.         try
  11.         {
  12.             Funct();
  13.         }
  14.         catch (const Test1&)
  15.         {
  16.             cerr << "Caught a exception" << endl;
  17.         }
  18.         return 0;
  19.     }
  20.     void Funct()
  21.     {
  22.         throw Test2();
  23.     }

a) Caught an exception
b) NULL
c) Both Caught an exception & NULL
d) Caught a exception
Answer: a
Clarification: In this program, We are arising with the exception by using the method in the class.
Output:

$ g++ uce3.cpp
$ a.out
Caught a exception

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

  1.     #include 
  2.     using namespace std;
  3.     #include 
  4.     #include 
  5.     void Funct()
  6.     {
  7.         cout << "Funct() was called by terminate()." << endl;
  8.         exit(0);
  9.     }
  10.     int main()
  11.     {
  12.         try
  13.         {
  14.             set_terminate(Funct);
  15.             throw "Out of memory!";
  16.         }
  17.         catch(int)
  18.         { 
  19.             cout << "Integer exception raised." << endl; 
  20.         }
  21.         return 0;
  22.     }

a) Integer exception raised
b) Funct() was called by terminate()
c) Integer exception not raised
d) Integer exception raised.
Answer: b
Clarification: As there is no integer in this program, We are printing Funct() was called by terminate().
Output:

$ g++ uce4.cpp
$ a.out
Funct() was called by terminate().

9. What function will be called when we have an uncaught exception?
a) catch
b) throw
c) terminate
d) try
Answer: c
Clarification: If we have an uncaught exception means, the compiler will throw the control of the program to terminate function.

10. What will not be called when the terminate() is raised in the constructor?
a) main()
b) class
c) destructor
d) constructor
Answer: c
Clarification: To free the memory occupied by that object during initializing and destroy that object.

  250+ TOP MCQs on Permutations and Answers

Leave a Comment

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

Scroll to Top