Exceptions That Are Not Errors(C++ Programming ) Questions and Answers

Question 1.

What kind of exceptions are available in c++?


  1.    handled
  2.    unhandled
  3.    static
  4.    dynamic
Explanation:-
Answer: Option B. -> unhandled

None



Question 2.

How to handle error in the destructor?


  1.    throwing
  2.    terminate
  3.    both a & b
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> terminate

It will not throw an exception from the destructor but it will the process by using terminate() function.



Question 3.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
void myunexpected ()
5.
{
6.
cout
  1.    caught other exception
  2.    caught int
  3.    unexpected called
  4.    both b & c
Explanation:-
Answer: Option D. -> both b & c

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



Question 4.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int x = -1;
6.
char *ptr;
7.
ptr = new char[256];
8.
try
9.
{
10.
if (x < 0)
11.
{
12.
throw x;
13.
}
14.
if (ptr == NULL)
15.
{
16.
throw " ptr is NULL ";
17.
}
18.
}
19.
catch (...)
20.
{
21.
cout
  1.    -1
  2.    ptr is NULL
  3.    Exception occured: exiting
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> Exception occured: exiting

catch(…) is used to catch all types of exceptions arising in the program.
Output:
$ g++ etea.cpp
$ a.out
Exception occured: exiting



Question 5.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
void myunexpected ()
5.
{
6.
cout
  1.    unexpected handler called
  2.    caught bad_exception
  3.    caught other exception
  4.    both a & b
Explanation:-
Answer: Option D. -> both a & b

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



Question 6.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
void myunexpected ()
5.
{
6.
cout
  1.    unexpected handler called
  2.    caught bad_exception
  3.    caught other exception
  4.    both a & b
Explanation:-
Answer: Option D. -> both a & b

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



Question 7.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
class Polymorphic {virtual void Member(){}};
5.
int main ()
6.
{
7.
try
8.
{
9.
Polymorphic * pb = 0;
10.
typeid(*pb);
11.
}
12.
catch (exception& e)
13.
{
14.
cerr
  1.    exception caught: std::bad_typeid
  2.    exception caught: std::bad_alloc
  3.    exception caught: std::bad_cast
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> exception caught: std::bad_typeid

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



Question 8.

Which type of program is recommended to include in try block?


  1.    static memory allocation
  2.    dynamic memory allocation
  3.    const reference
  4.    pointer
Explanation:-
Answer: Option B. -> dynamic memory allocation

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.



Question 9.

Which statement is used to catch all types of exceptions?


  1.    catch()
  2.    catch(Test t)
  3.    catch(----)
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> catch(----)

This catch statement will catch all types of exceptions that arises in the program.



Question 10.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int x = -1;
6.
try
7.
{
8.
if (x < 0)
9.
{
10.
throw x;
11.
}
12.
else
13.
{
14.
cout
  1.    -1
  2.    0
  3.    Exception occurred: Thrown value is -1
  4.    error
Explanation:-
Answer: Option C. -> Exception occurred: Thrown value is -1

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