C++ Programming Questions and Answers – Error Handling

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

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

1. Which keyword is used to handle the expection?
a) try
b) throw
c) catch
d) handler
View Answer

Answer: c
Explanation: When we found a exception in the program, We need to throw that and we handle that by using the catch keyword.

2. Which is used to throw a exception?
a) throw
b) try
c) catch
d) handler
View Answer

Answer: a
Explanation: throw keyword is used to throw an exception.
eg:

if(divisor == 0)
{
    throw "Divide by zero error";
}

3. What is the use of the ‘finally’ keyword?
a) It used to execute at the starting of the program
b) It will be executed at the end of the program even if the exception arised
c) It will be executed at the starting of the program even if the exception arised
d) It will be executed at the middle of the program even if the exception arised
View Answer

Answer: b
Explanation: finally keyword will be executed at the end of all the exception.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     double division(int a, int b)
  4.     {
  5.         if (b == 0)
  6.         {
  7.             throw "Division by zero condition!";
  8.         }
  9.         return (a / b);
  10.     }
  11.     int main ()
  12.     {
  13.         int x = 50;
  14.         int y = 0;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z << endl;
  20.         }
  21.         catch (const char* msg) 
  22.         {
  23.             cerr << msg << endl;
  24.         }
  25.         return 0;
  26.     }

a) 50
b) 0
c) Division by zero condition!
d) Error
View Answer

Answer: c
Explanation: It’s a mathematical certainty, We can’t divide by zero, So we’re arising a exception.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ excep.cpp
$ a.out
Division by zero condition!

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main () 
  4.     {
  5.         try
  6.         {
  7.             throw 20;
  8.         }
  9.         catch (int e)
  10.         {
  11.             cout << "An exception occurred " << e << endl;
  12.         }
  13.         return 0;
  14.     }

a) 20
b) An exception occurred
c) Error
d) An exception occurred 20
View Answer

Answer: d
Explanation: We are handling the exception by throwing that number. So the output is printed with the given number.
Output:

advertisement
$ g++ excep1.cpp
$ a.out
An exception occurred 20

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

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     class myexception: public exception
  5.     {
  6.         virtual const char* what() const throw()
  7.         {
  8.             return "My exception";
  9.         }
  10.     } myex;
  11.     int main () 
  12.     {
  13.         try
  14.         {
  15.             throw myex;
  16.         }
  17.         catch (exception& e)
  18.         {
  19.             cout << e.what() << endl;
  20.         }
  21.         return 0;
  22.     }

a) Exception
b) Error
c) My exception
d) Runtime error
View Answer

Answer: c
Explanation: This is a standard exception handler used in the class.
Output:

$ g++ excep2.cpp
$ a.out
My exception

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

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         try
  7.         {
  8.             int* myarray = new int[1000];
  9.             cout << "allocated";
  10.         }
  11.         catch (exception& e)
  12.         {
  13.             cout << "Standard exception: " << e.what() << endl;
  14.         }
  15.         return 0;
  16.     }

a) Allocated
b) Standard exception
c) Depends on the memory
d) Error
View Answer

Answer: c
Explanation: In this program, We are allocating the memory for array. If it is allocated means, no exception will arise and if there is no size in memory means, Exception will arise.
Output:

$ g++ excep3.cpp
$ a.out
allocated

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

  1.     #include <iostream>
  2.     #include <exception>
  3.     using namespace std;
  4.     struct MyException : public exception
  5.     {
  6.         const char * what () const throw ()
  7.         {
  8.             return "C++ Exception";
  9.         }
  10.     };
  11.     int main()
  12.     {
  13.         try
  14.         {
  15.             throw MyException();
  16.         }
  17.         catch(MyException& e)
  18.         {
  19.             cout << "Exception caught" << std::endl;
  20.             cout << e.what() << std::endl;
  21.         }
  22.         catch(std::exception& e)
  23.         {
  24.         }    
  25.     }

a) C++ Exception
b) Exception caught
c)

   Exception caught
   C++ Exception

d) Error
View Answer

Answer: c
Explanation: We are defining the user-defined exception in this program.
Output:

$ g++ excep4.cpp
$ a.out
C++ Exception
Exception caught

9. How do define the user-defined exceptions?
a) inheriting and overriding exception class functionality
b) overriding class functionality
c) inheriting class functionality
d) delting and adding class member
View Answer

Answer: a
Explanation: User defined exceptions can be done by inheriting and overriding the exception class functionality.

10. Which exception is thrown by dynamic_cast?
a) bad_cast
b) bad_typeid
c) bad_exception
d) bad_alloc
View Answer

Answer: a
Explanation: bad_cast exception is thrown by dynamic_cast.

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.