Log In Register

Units


Error Handling - Questions and Answers

 

Practice these Error Handling under C++ Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Error Handling for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Error Handling of C++ Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge C++ Programming specifically Error Handling.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Error Handling under C++ Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Error Handling topic of C++ Programming:

 

1.

What is the output of this program?

  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.     }

Answer: Option 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

2.

What is the use of the ‘finally’ keyword?

Answer: Option B

Explanation:

finally keyword will be executed at the end of all the exception.

3.

How do define the user-defined exceptions?

Answer: Option A

Explanation:

4.

Which keyword is used to handle the expection?

Answer: Option C

Explanation:

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

5.

What is the output of this program?

  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.     }

Answer: Option C

Explanation:

It’s a mathematical certainty, We can’t divide by zero, So we’re arising a exception.
Output:
$ g++ excep.cpp
$ a.out
Division by zero condition!

6.

Which exception is thrown by dynamic_cast?

Answer: Option A

Explanation:

bad_cast exception is thrown by dynamic_cast.

7.

What is the output of this program?

  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.     }

Answer: Option D

Explanation:

We are handling the exception by throwing that number. So the output is printed with the given number.
Output:
$ g++ excep1.cpp
$ a.out
An exception occurred 20

8.

What is the output of this program?

  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.     }

Answer: Option C

Explanation:

We are defining the user-defined exception in this program.
Output:
$ g++ excep4.cpp
$ a.out
C++ Exception
Exception caught

9.

What is the output of this program?

  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.     }

Answer: Option C

Explanation:

This is a standard exception handler used in the class.
Output:
$ g++ excep2.cpp
$ a.out
My exception

10.

Which is used to throw a exception?

Answer: Option A

Explanation: