250+ TOP MCQs on Exceptions and Efficiency and Answers

This section on C++ test on “Exceptions and Efficiency”. One shall practice these test questions 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++ test questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

1. What will happen when we move to try block far away from catch block?
a) Reduces the amount of code in cache
b) Increases the amount of code in cache
c) Don’t alter anything
d) Increases the amount of code
Answer: a
Clarification: compilers may try to move the catch-code far away from the try-code, which reduces the amount of code to keep in cache normally, thus enhancing performance.

2. What will happen if an exception that is thrown may cause a whole load of objects to go out of scope?
a) Terminate the program
b) Produce a runtime error
c) It will be added to the overhead
d) Compilation error
Answer: c
Clarification: It will be added to the overhead if an exception that is thrown may cause a whole load of objects to go out of scope.

3. What operation can be performed by destructor?
a) Abort the program
b) Resource cleanup
c) Exit from the current block
d) Terminate the program

Answer: b
Clarification: It will be used to free all the resources that are used by the block of code during execution.

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

a) Memory allocated
b) Exception arised
c) Depends on the computer memory
d) Memory allocatedException arised
Answer: c
Clarification: The value will be allocated, if there is enough memory in the system.
Output:

$ g++ expef.cpp
$ a.out
Memory allocated

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

  1.     #include 
  2.     using namespace std;
  3.     void test(int x)
  4.     {
  5.         try
  6.         {
  7.             if (x > 0)
  8.                 throw x;
  9.             else
  10.                 throw 'x';
  11.         }
  12.         catch(char)
  13.         {
  14.             cout << "Catch a integer and that integer is:" << x;
  15.         }
  16.     }
  17.     int main()
  18.     {
  19.         cout << "Testing multiple catchesn:";
  20.         test(10);
  21.         test(0);
  22.     }

a) Catch a integer and that integer is:10
b) Error
c) Runtime error
d) Catch a integer and that integer is:25
Answer: c
Clarification: As the catch is created with a wrong type, So it will
arise a runtime error.
Output:

$ g++ expef.cpp
$ a.out
Testing multiple catches
terminate called after throwing an instance of 'int'
:Aborted

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

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

a) Invalid arguments
b) Executed
c) Error
d) Runtime error
Answer: b
Clarification: As we are throwing the function and catching it with a correct data type, So this program will execute.
Output:

$ g++ expef.cpp
$ a.out
Executed

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

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

a) Exception is 5
b) Exception is 1.1f
c) Wrong number used
d) Exception is 1.6g
Answer: c
Clarification: As we are giving 3 to num, It is arising an exception named
“wrong number used”.
Output:

$ g++ expef.cpp
$ a.out
wrong number used

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

a) 50
b) 0
c) Division by zero condition
d) Error
Answer: d
Clarification: As we missed the data type in the catch block, It will arise an error.

9. What is the main purpose of the constructor?
a) Begin the execution of class
b) Include the macros for the program
c) Establish the class invariant
d) Terminate the program
Answer: c
Clarification: The purpose of a constructor is to establish the class invariant. To do that, it often needs to acquire system resources or in general perform an operation that may fail.

10. Why is it expensive to use objects for the exception?
a) Exception object is created only if an error actually happens
b) Because of execution time
c) Memory space involved in creating an exception object
d) Because of time and space
Answer: a
Clarification: If an error occurs in the program, then only exception object is created otherwise, It will not be created. since throwing an exception triggers a bunch of actions during the stack unrolling, like invoking the the destructor of all the objects that has been created up to the point in which we are able to catch the exception, and invoking the destructor methods can imply flushing streams and freeing memory which can be expensive as well. Therefore, it’s expensive to use in the program.

Leave a Comment

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

Scroll to Top