Exceptions(C++ Programming ) Questions and Answers

Question 1.

What will happen when the handler is not found for exception?


  1.    Calls the standard library function terminate()
  2.    raise an error
  3.    executes the remaining block
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> Calls the standard library function terminate()

None.



Question 2.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
int main()
5.
{
6.
try {
7.
int * array1 = new int[100000000];
8.
int * array2 = new int[100000000];
9.
int * array3 = new int[100000000];
10.
int * array4 = new int[100000000];
11.
cout
  1.    Allocated successfully
  2.    error allocating the requested memory
  3.    Depends on the memory of the computer
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> Depends on the memory of the computer

In this program, we allocating the memory to the arrays by using excetion handling and 

we handled the exception by standard exception.
Output:
$ g++ excep5.cpp
$ a.out
Allocated successfully




Question 3.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
void Funct();
4.
int main()
5.
{
6.
try {
7.
Funct();
8.
}
9.
catch(double) {
10.
cerr
  1.    caught a double type
  2.    compile time error
  3.    abnormal program termination
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> abnormal program termination

As we are throwing integer to double it will raise as abnormal program after termination throw 

statement.
Output:
$ g++ excep4.cpp
$ a.out
terminate called after throwing an instance of ‘int’
Aborted



Question 4.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
char* buff;
6.
try {
7.
buff = new char[1024];
8.
if (buff == 0)
9.
throw "Memory allocation failure!";
10.
else
11.
cout
  1.    4 Bytes allocated successfully
  2.    8 Bytes allocated successfully
  3.    Memory allocation failure
  4.    depends on the size of data type
Explanation:-
Answer: Option D. -> depends on the size of data type

As we are allocating the memory to the variables and if there is not suffcient size means, it 

will throw an exception.
Output:
$ g++ excep3.cpp
$ a.out
4 Bytes allocated successfully



Question 5.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
double division(int a, int b)
4.
{
5.
if (b == 0) {
6.
throw "Division by zero condition!";
7.
}
8.
return (a / b);
9.
}
10.
int main ()
11.
{
12.
int x = 50;
13.
int y = 2;
14.
double z = 0;
15.
try {
16.
z = division(x, y);
17.
cout
  1.    25
  2.    20
  3.    Division by zero condition!
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 25

In this program, we resembling the division by using the exception handling.
Output:
$ g++ excep2.cpp
$ a.out
25



Question 6.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
void PrintSequence(int StopNum)
4.
{
5.
int Num;
6.
Num = 1;
7.
while (true) {
8.
if (Num >= StopNum)
9.
throw Num;
10.
cout
  1.    compile time error
  2.    prints first 19 numbers
  3.    prints first 19 numbers and throws exception at 20
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> prints first 19 numbers and throws exception at 20

In this program, we are printing upto 19 numbers and when executing the 20, we are raising 

a exception.
Output:
$ g++ excep1.cpp
$ a.out
12345678910111213141516171819Caught an exception with value: 20



Question 7.

What will happen when the exception is not caught in the program?


  1.    error
  2.    program will execute
  3.    block of that code will not execute
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> error

None.



Question 8.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int age = 0;
6.
try {
7.
if (age < 0) {
8.
throw "Positive Number Required";
9.
}
10.
cout
  1.    0
  2.    Error:Positive Number Required
  3.    compile time error
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 0

As the zero marks the beginning of the positive number, it is printed as output
Output:
$ g++ excep.cpp
$ a.out
0



Question 9.

To where does the program control transfers when exception is arised?


  1.    catch
  2.    handlers
  3.    throw
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> handlers

When a exception is arised mean, the exception is caught by handlers and then it decides 

the type of exception.



Question 10.

Which key word is used to check exception in the block of code?


  1.    catch
  2.    throw
  3.    try
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> try

The try() statement is used for exceptios in c++.