C++ Programming Questions and Answers – Exceptions and Efficiency

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

Here is a listing of C++ 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

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

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

  1.     #include <iostream>
  2.     #include <exception>
  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
View Answer

Answer: c
Explanation: 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?

advertisement
  1.     #include <iostream>
  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 catches\n:";
  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
View Answer

Answer: c
Explanation: As the catch is created with a wrong type, So it will
arise a runtime error.
Output:

advertisement
$ 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 <stdexcept>
  2.     #include <limits>
  3.     #include <iostream>
  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
View Answer

Answer: b
Explanation: 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 <iostream>
  2.     #include <string>
  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
View Answer

Answer: c
Explanation: 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 <iostream>
  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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.