CPP

CPP

Exceptions has 10 Questions

To where does the program control transfers when exception is arised?

catch
handlers
throw
none of the mentioned
Which key word is used to check exception in the block of code?

catch
throw
try
none of the mentioned
What will happen when the exception is not caught in the program?

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

0
Error:Positive Number Required
compile time error
none of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     void PrintSequence(int StopNum)
  4.     {
  5.         int Num;
  6.         Num = 1;
  7.         while (true) {
  8.             if (Num >= StopNum)
  9.                 throw Num;
  10.             cout << Num;
  11.             Num++;
  12.         }
  13.     }
  14.     int main(void)
  15.     {
  16.         try {
  17.             PrintSequence(20);
  18.         }
  19.         catch(int ExNum)
  20.         {
  21.             cout << "Caught an exception with value: " << ExNum;
  22.         }
  23.         return 0;
  24.     }

compile time error
prints first 19 numbers
prints first 19 numbers and throws exception at 20
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.             throw "Division by zero condition!";
  7.         }
  8.         return (a / b);
  9.     }
  10.     int main ()
  11.     {
  12.         int x = 50;
  13.         int y = 2;
  14.         double z = 0;
  15.         try {
  16.             z = division(x, y);
  17.             cout << z;
  18.         }
  19.         catch(const char *msg) {
  20.             cerr << msg;
  21.         }
  22.         return 0;
  23.     }

25
20
Division by zero condition!
none of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char* buff;
  6.         try {
  7.             buff = new char[1024];
  8.             if (buff == 0)
  9.                throw "Memory allocation failure!";
  10.             else
  11.                cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
  12.         }
  13.         catch(char *strg) {
  14.             cout<<"Exception raised: "<<strg<<endl;
  15.         }
  16.         return 0;
  17.     }

4 Bytes allocated successfully
8 Bytes allocated successfully
Memory allocation failure
depends on the size of data type
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     void Funct();
  4.     int main()
  5.     {
  6.         try {
  7.             Funct();
  8.         }
  9.         catch(double) {
  10.             cerr << "caught a double type..." << endl;
  11.         }
  12.         return 0;
  13.     }
  14.     void Funct()
  15.     {
  16.         throw 3;
  17.     }

caught a double type
compile time error
abnormal program termination
none of the mentioned
What will happen when the handler is not found for exception?

Calls the standard library function terminate()
raise an error
executes the remaining 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.             int * array1 = new int[100000000];
  8.             int * array2 = new int[100000000];
  9.             int * array3 = new int[100000000];
  10.             int * array4 = new int[100000000];
  11.             cout << "Allocated successfully";
  12.         }
  13.         catch(bad_alloc&) {
  14.             cout << "Error allocating the requested memory." << endl;
  15.         }
  16.         return 0;
  17.     }

Allocated successfully
error allocating the requested memory
Depends on the memory of the computer
none of the mentioned