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
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
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
Explanation: 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?
#include <iostream>
#include <exception>
using namespace std;
int main ()
{
try
{
double* i= new double[1000];
cout << "Memory allocated";
}
catch (exception& e)
{
cout << "Exception arised: " << e.what() << endl;
}
return 0;
}
a) Memory allocated
b) Exception arised
c) Depends on the computer memory
d) Memory allocatedException arised
View Answer
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?
#include <iostream>
using namespace std;
void test(int x)
{
try
{
if (x > 0)
throw x;
else
throw 'x';
}
catch(char)
{
cout << "Catch a integer and that integer is:" << x;
}
}
int main()
{
cout << "Testing multiple catches\n:";
test(10);
test(0);
}
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
Explanation: 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?
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void func(int c)
{
if (c < numeric_limits<char> :: max())
throw invalid_argument("MyFunc argument too large.");
else
{
cout<<"Executed";
}
}
int main()
{
try
{
func(256);
}
catch(invalid_argument& e)
{
cerr << e.what() << endl;
return -1;
}
return 0;
}
a) Invalid arguments
b) Executed
c) Error
d) Runtime error
View Answer
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?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int num = 3;
string str_bad = "wrong number used";
try
{
if ( num == 1 )
{
throw 5;
}
if ( num == 2 )
{
throw 1.1f;
}
if ( num != 1 || num != 2 )
{
throw str_bad;
}
}
catch (int a)
{
cout << "Exception is: " << a << endl;
}
catch (float b)
{
cout << "Exception is: " << b << endl;
}
catch (...)
{
cout << str_bad << endl;
}
return 0;
}
a) Exception is 5
b) Exception is 1.1f
c) Wrong number used
d) Exception is 1.6g
View Answer
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?
#include <iostream>
using namespace std;
double division(int a, int b)
{
if (b == 0)
{
throw "Division by zero condition!";
}
return (a / b);
}
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try
{
z = division(x, y);
cout << z << endl;
}
catch (const msg)
{
cerr << msg << endl;
}
return 0;
}
a) 50
b) 0
c) Division by zero condition
d) Error
View Answer
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
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
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.
- Check C++ Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check Programming Books
- Practice Computer Science MCQs