C++ Programming Questions and Answers – Exceptions That Are Not Errors

This section on C++ MCQs (multiple choice questions) focuses 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
View Answer

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

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

Answer: c
Explanation: This catch statement will catch all types of exceptions that arises in the program.
advertisement
advertisement

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

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

Answer: c
Explanation: As the given value is -1 and according to the condition, We are arising an exception.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ etae.cpp
$ a.out
Exception occurred: Thrown value is -1

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

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

Answer: a
Explanation: In this program, We used a bad type id for the polymorphic operator, So it is arising an bad_typeid exception.
Output:

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

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

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     void myunexpected () 
  5.     {
  6.         cout << "unexpected handler called\n";
  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 int\n"; 
  23.         }
  24.         catch (bad_exception be) 
  25.         { 
  26.             cout << "caught bad_exception\n"; 
  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
View Answer

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

Answer: c
Explanation: 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 <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     void myunexpected ()
  5.     {
  6.         cout << "unexpected called\n";
  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 int\n";
  23.         }
  24.         catch (...)  
  25.         { 
  26.             cout << "caught other exception\n"; 
  27.         }
  28.         return 0;
  29.     }

a) caught other exception
b) caught int
c) unexpected called
d) both caught int & unexpected called
View Answer

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

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

Answer: b
Explanation: unhandled kind of exceptions are available in c++.

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.