C++ Programming Questions and Answers – Uncaught Exceptions

This section on C++ programming questions and answers focuses 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: The ellipses operator can be represented as (…).
advertisement
advertisement

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

  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: As we are throwing a negative number and we are using the only integer, So it is arising an error.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ uce.cpp
$ a.out
terminate called after throwing an instance of 'int'

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

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

d)

   one 
   terminate
   Aborted
View Answer
Answer: d
Explanation: 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 <iostream>
  2.     #include <exception>
  3.     #include <cstdlib>
  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
View Answer

Answer: c
Explanation: 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 <iostream>
  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
View Answer

Answer: a
Explanation: 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 <iostream>
  2.     using namespace std;
  3.     #include <cstdlib>
  4.     #include <exception>
  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.
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: To free the memory occupied by that object during initializing and destroy that object.

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.