250+ TOP MCQs on Exceptions and Answers

This section on C++ Programming quiz on “Exceptions”. One shall practice these quizzes to improve their C++ programming skills needed for various interviews (campus interviews, walk-in 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++ quiz comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Programming quiz on “Exceptions” along with answers, explanations and/or solutions:

1. To where does the program control transfers when the exception is arisen?
a) catch
b) handlers
c) throw
d) try
Answer: b
Clarification: When an exception is arisen mean, the exception is caught by handlers and then it decides the type of exception.

2. Which keyword is used to check exception in the block of code?
a) catch
b) throw
c) try
d) handlers
Answer: c
Clarification: The try() statement is used for exceptions in c++.

3. What will happen when the exception is not caught in the program?
a) error
b) program will execute
c) block of that code will not execute
d) program will execute & displays wrong output
Answer: a
Clarification: When exceptions are not caught in any program then program throws error.

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

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int age = 0;
  6.         try 
  7.         {
  8.             if (age < 0) 
  9.             {
  10.                 throw "Positive Number Required";
  11.             }
  12.             cout << age;
  13.         }
  14.         catch(const char *Message)
  15.         {
  16.             cout << "Error: " << Message;
  17.         }
  18.         return 0;
  19.     }

a) 0
b) error:Positive Number Required
c) compile time error
d) runtime error
Answer: a
Clarification: As the zero marks the beginning of the positive number, it is printed as output
Output:

$ g++ excep.cpp
$ a.out
0

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

  1.     #include 
  2.     using namespace std;
  3.     void PrintSequence(int StopNum)
  4.     {
  5.         int Num;
  6.         Num = 1;
  7.         while (true) 
  8.         {
  9.             if (Num >= StopNum)
  10.                 throw Num;
  11.             cout << Num;
  12.             Num++;
  13.         }
  14.     }
  15.     int main(void)
  16.     {
  17.         try 
  18.         {
  19.             PrintSequence(20);
  20.         }
  21.         catch(int ExNum)
  22.         {
  23.             cout << "Caught an exception with value: " << ExNum;
  24.         }
  25.         return 0;
  26.     }

a) compile time error
b) prints first 19 numbers
c) prints first 19 numbers and throws exception at 20
d) prints first 17 numbers
Answer: c
Clarification: In this program, we are printing upto 19 numbers and when executing the 20, we are raising a exception.
Output:

$ g++ excep1.cpp
$ a.out
12345678910111213141516171819Caught an exception with value: 20

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

  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 = 2;
  15.         double z = 0;
  16.         try 
  17.         {
  18.             z = division(x, y);
  19.             cout << z;
  20.         }
  21.         catch(const char *msg) 
  22.         {
  23.             cerr << msg;
  24.         }
  25.         return 0;
  26.     }

a) 25
b) 20
c) Division by zero condition!
d) 35
Answer: a
Clarification: In this program, we resembling the division by using the exception handling.
Output:

$ g++ excep2.cpp
$ a.out
25

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

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char* buff;
  6.         try 
  7.         {
  8.             buff = new char[1024];
  9.             if (buff == 0)
  10.                throw "Memory allocation failure!";
  11.             else
  12.                cout << sizeof(buff) << "Byte successfully allocated!"<<endl;
  13.         }
  14.         catch(char *strg)
  15.         {
  16.             cout<<"Exception raised: "<<strg<<endl;
  17.         }
  18.         return 0;
  19.     }

a) 4 Bytes allocated successfully
b) 8 Bytes allocated successfully
c) Memory allocation failure
d) Depends on the size of the data type
Answer: d
Clarification: As we are allocating the memory to the variables and if there are not sufficient size means, it will throw an exception.
Output:

$ g++ excep3.cpp
$ a.out
4 Bytes allocated successfully

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

  1.     #include 
  2.     using namespace std;
  3.     void Funct();
  4.     int main()
  5.     {
  6.         try 
  7.         {
  8.             Funct();
  9.         }
  10.         catch(double) 
  11.         {
  12.             cerr << "caught a double type..." << endl;
  13.         }
  14.         return 0;
  15.     }
  16.     void Funct()
  17.     {
  18.         throw 3;
  19.     }

a) caught a double type
b) compile time error
c) abnormal program termination
d) runtime error
Answer: c
Clarification: As we are throwing integer to double it will raise as abnormal program after termination throw statement.
Output:

$ g++ excep4.cpp
$ a.out
terminate called after throwing an instance of 'int'
Aborted

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         try 
  7.         {
  8.             int * array1 = new int[100000000];
  9.             int * array2 = new int[100000000];
  10.             int * array3 = new int[100000000];
  11.             int * array4 = new int[100000000];
  12.             cout << "Allocated successfully";
  13.         }
  14.         catch(bad_alloc&) 
  15.         {
  16.             cout << "Error allocating the requested memory." << endl;
  17.         }
  18.         return 0;
  19.     }

a) Allocated successfully
b) Error allocating the requested memory
c) Depends on the memory of the computer
d) Error
Answer: c
Clarification: In this program, we allocating the memory to the arrays by using exception handling and we handled the exception by standard exception.
Output:

$ g++ excep5.cpp
$ a.out
Allocated successfully

10. What will happen when the handler is not found for an exception?
a) calls the standard library function terminate()
b) raise an error
c) executes the remaining block
d) raise an error and executes the remaining block
Answer: a
Clarification: None.

  250+ TOP MCQs on Tuples – 2 and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top