CPP

CPP

Exceptions and Efficiency has 8 Questions

What will happen when we move try block far away from catch block?

Reduces the amount of code in cache
Increases the amount of code in cache
Don't alter anything
None of the mentioned
What will happen if an excpetion that is thrown may causes a whole load of objects to go out of scope?

Terminate the program
Produce a runtime error
It will be added to the overhead
None of the mentioned
What operation can be performed by destructor?

Abort the program
Resource cleanup
Exit from the current block
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         try
  7.         {
  8.             double* i= new double[1000];
  9.             cout << "Memory allocated";
  10.         }
  11.         catch (exception& e)
  12.         {
  13.             cout << "Exception arised: " << e.what() << endl;
  14.         }
  15.         return 0;
  16.     }

Memory allocated
Exception arised
Depends on the computer memory
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     void func(int c)
  6.     {
  7.         if (c < numeric_limits<char> :: max())
  8.             throw invalid_argument("MyFunc argument too large.");
  9.         else
  10.         {
  11.             cout<<"Executed";
  12.         }
  13.     }
  14.     int main()
  15.     {
  16.         try
  17.         {
  18.             func(256);
  19.         }
  20.         catch(invalid_argument& e)
  21.         {
  22.             cerr << e.what() << endl;
  23.             return -1;
  24.         }
  25.         return 0;
  26.     }

Invalid arguments
Executed
Error
Runtime error
What is the output of this program?
  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         int num = 3;
  7.         string str_bad = "wrong number used";
  8.         try
  9.         {
  10.             if ( num == 1 )
  11.             {       
  12.                 throw 5;
  13.             }
  14.             if ( num == 2 )
  15.             {
  16.                 throw 1.1f;
  17.             }
  18.             if ( num != 1 || num != 2 )
  19.             {    
  20.                 throw str_bad;
  21.             }
  22.         }
  23.         catch (int a)
  24.         {
  25.             cout << "Exception is: " << a << endl;
  26.         }
  27.         catch (float b)
  28.         {
  29.             cout << "Exception is: " << b << endl;
  30.         }
  31.         catch (...)
  32.         {
  33.             cout  << str_bad << endl;
  34.         }
  35.         return 0;
  36.     }

Exception is 5
Exception is 1.1f
wrong number used
None of the mentioned
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 msg) 
  22.         {
  23.             cerr << msg << endl;
  24.         }
  25.         return 0;
  26.     }

50
0
Division by zero condition
Error
What is the main purpose of the constructor?

Begin the execution of the class
Include the macros for the program
Establish the class invariant
None of the mentioned