CPP

CPP

Catching Exceptions has 9 Questions

How many parameters does the throw expression can have?

1
2
3
4
Where exception are handled?

inside the program
outside the regular code
both inside or outside
none of the mentioned
Which is used to check the error in the block?

try
throw
catch
none of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     class myexception: public exception
  5.     {
  6.         virtual const char* what() const throw()
  7.         {
  8.             return "exception arised";
  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.     }

exception arised
error
exception
runtime error
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int age=5;
  6.         try 
  7.         {
  8.             if (age < 0)
  9.                 throw "Positive Number Required";
  10.             cout  << age << "\n\n";
  11.         }
  12.         catch(const char* Message)
  13.         {
  14.             cout << "Error: " << Message;
  15.         }
  16.         return 0;
  17.     }

5
10
15
Positive Number Required
What should present when throwing a object?

constructor
copy-constructor
destructor
none of the mentioned
How to handle the exception in constructor?

We have to throw an exception
We have to return the exception
both a & b
none of the mentioned
What is the output of this program?
  1.     #include
  2.     #include "math.h"
  3.     using namespace std;
  4.     double MySqrt(double d)
  5.     {
  6.         if (d < 0.0)
  7.         throw "Cannot take sqrt of negative number";     
  8.         return sqrt(d);
  9.     }
  10.     int main()
  11.     {
  12.         double d = 5;
  13.         cout << MySqrt(d) << endl;
  14.     }

5
2.236
error
Cannot take sqrt of negative number
What is the output of this program?
  1.     #include 
  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.             cout << msg << endl;
  24.         }
  25.         return 0;
  26.     }

50
0
Division by zero condition!
none of the mentioned