Here is a listing of C multiple choice questions on “Exceptions That Are Not Errors” along with answers, explanations and/or solutions:
1. Which is used to handle the exceptions in c++?
a) catch handler
b) handler
c) exception handler
d) throw
View Answer
Explanation: Exception handler is used to handle the exceptions in c++.
2. Which type of program is recommended to include in try block?
a) static memory allocation
b) dynamic memory allocation
c) const reference
d) pointer
View Answer
Explanation: While during dynamic memory allocation, Your system may not have sufficient resources to handle it, So it is better to use it inside the try block.
3. Which statement is used to catch all types of exceptions?
a) catch()
b) catch(Test t)
c) catch(…)
d) catch(Test)
View Answer
Explanation: This catch statement will catch all types of exceptions that arises in the program.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
try
{
if (x < 0)
{
throw x;
}
else
{
cout<<x;
}
}
catch (int x )
{
cout << "Exception occurred: Thrown value is " << x << endl;
}
return 0;
}
a) -1
b) 0
c) Exception occurred: Thrown value is -1
d) Error
View Answer
Explanation: As the given value is -1 and according to the condition, We are arising an exception.
Output:
$ g++ etae.cpp $ a.out Exception occurred: Thrown value is -1
5. What will be the output of the following C++ code?
#include <iostream>
#include <typeinfo>
using namespace std;
class Polymorphic {virtual void Member(){}};
int main ()
{
try
{
Polymorphic * pb = 0;
typeid(*pb);
}
catch (exception& e)
{
cerr << "exception caught: " << e.what() << endl;
}
return 0;
}
a) exception caught: std::bad_typeid
b) exception caught: std::bad_alloc
c) exception caught: std::bad_cast
d) exception caught: std::bad_id
View Answer
Explanation: In this program, We used a bad type id for the polymorphic operator, So it is arising an bad_typeid exception.
Output:
$ g++ etae.cpp $ a.out exception caught: std::bad_typeid
6. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
void myunexpected ()
{
cout << "unexpected handler called\n";
throw;
}
void myfunction () throw (int,bad_exception)
{
throw 'x';
}
int main (void)
{
set_unexpected (myunexpected);
try
{
myfunction();
}
catch (int)
{
cout << "caught int\n";
}
catch (bad_exception be)
{
cout << "caught bad_exception\n";
}
catch (...)
{
cout << "caught other exception \n";
}
return 0;
}
a) unexpected handler called
b) caught bad_exception
c) caught other exception
d) both unexpected handler called & caught bad_exception
View Answer
Explanation: In this program, We are calling set_unexpected and myfunction, So it is printing the output as the given.
Output:
$ g++ etae.cpp $ a.out unexpected handler called caught bad_exception
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
char *ptr;
ptr = new char[256];
try
{
if (x < 0)
{
throw x;
}
if (ptr == NULL)
{
throw " ptr is NULL ";
}
}
catch (...)
{
cout << "Exception occurred: exiting "<< endl;
}
return 0;
}
a) -1
b) ptr is NULL
c) exception occured: exiting
d) 1
View Answer
Explanation: catch(…) is used to catch all types of exceptions arising in the program.
Output:
$ g++ etea.cpp $ a.out Exception occured: exiting
8. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
void myunexpected ()
{
cout << "unexpected called\n";
throw 0;
}
void myfunction () throw (int)
{
throw 'x';
}
int main ()
{
set_unexpected (myunexpected);
try
{
myfunction();
}
catch (int)
{
cout << "caught int\n";
}
catch (...)
{
cout << "caught other exception\n";
}
return 0;
}
a) caught other exception
b) caught int
c) unexpected called
d) both caught int & unexpected called
View Answer
Explanation: As we are calling set_unexpected (myunexpected) function, this is printing as unexpected called and because of operator compliance it is arising an exception.
Output:
$ g++ etea.cpp $ a.out unexpected called caught int
9. How to handle error in the destructor?
a) throwing
b) terminate
c) both throwing & terminate
d) try
View Answer
Explanation: It will not throw an exception from the destructor but it will the process by using terminate() function.
10. What kind of exceptions are available in c++?
a) handled
b) unhandled
c) static
d) dynamic
View Answer
Explanation: unhandled kind of exceptions are available in c++.
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 Computer Science Books
- Check C++ Books
- Apply for Computer Science Internship
- Practice Programming MCQs
- Apply for C++ Internship