Catching Exceptions(C++ Programming ) Questions and Answers

Question 1.

What should present when throwing a object?


  1.    constructor
  2.    copy-constructor
  3.    destructor
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> copy-constructor

None.



Question 2.

How to handle the exception in constructor?


  1.    We have to throw an exception
  2.    We have to return the exception
  3.    both a & b
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> We have to throw an exception

As a constructor don't have a return type, We have to throw the exception.




Question 3.


What is the output of this program?


1.
#include
2.
#include "math.h"
3.
using namespace std;
4.
double MySqrt(double d)
5.
{
6.
if (d < 0.0)
7.
throw "Cannot take sqrt of negative number";
8.
return sqrt(d);
9.
}
10.
int main()
11.
{
12.
double d = 5;
13.
cout
  1.    5
  2.    2.236
  3.    error
  4.    Cannot take sqrt of negative number
Explanation:-
Answer: Option B. -> 2.236

We are finding the square root of the number, if it is a positive number, it can manipulate, Otherwise 

it will arise a exception.
Output:
$ g++ goe4.cpp
$ a.out
2.236



Question 4.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
int main()
5.
{
6.
double Op1 = 10, Op2 = 5, Res;
7.
char Op;
8.
try
9.
{
10.
if (Op != '+' && Op != '-' && Op != '*' && Op != '/')
11.
throw Op;
12.
switch(Op)
13.
{
14.
case '+':
15.
Res = Op1 + Op2;
16.
break;
17.
case '-':
18.
Res = Op1 - Op2;
19.
break;
20.
case '*':
21.
Res = Op1 * Op2;
22.
break;
23.
case '/':
24.
Res = Op1 / Op2;
25.
break;
26.
}
27.
cout
  1.    15
  2.    5
  3.    2
  4.    is not a valid operator
Explanation:-
Answer: Option D. -> is not a valid operator

It will arise a exception because we missed a operator.
Output:
$ g++ goe3.cpp
$ a.out
is not a valid operator



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.
{
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
  1.    50
  2.    0
  3.    Division by zero condition!
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> Division by zero condition!

We are dividing the values and if one of the values is zero means, We are arising an exception.
Output:
$ g++ goe2.cpp
$ a.out
Division by zero condition!




Question 6.


What is the output of this program?


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

In this program, We are checking the age of a person, If it is zero means, We will arise a exception.
Output:
$ g++ goe1.cpp
$ a.out
5



Question 7.


What is the output of this program?


1.
#include
2.
#include
3.
using namespace std;
4.
class myexception: public exception
5.
{
6.
virtual const char* what() const throw()
7.
{
8.
return "exception arised";
9.
}
10.
} myex;
11.
int main ()
12.
{
13.
try
14.
{
15.
throw myex;
16.
}
17.
catch (exception& e)
18.
{
19.
cout
  1.    exception arised
  2.    error
  3.    exception
  4.    runtime error
Explanation:-
Answer: Option A. -> exception arised

In this program, We are arising a standard exception and catching that and returning a statement.
Output:
$ g++ goe.cpp
$ a.out
exception arised



Question 8.

How many parameters does the throw expression can have?


  1.    1
  2.    2
  3.    3
  4.    4
Explanation:-
Answer: Option A. -> 1

In c++ program, We can be able to throw only one error at a time.



Question 9.

Where exception are handled?


  1.    inside the program
  2.    outside the regular code
  3.    both inside or outside
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> outside the regular code

none



Question 10.

Which is used to check the error in the block?


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

The try block is used to check for errors, if there is any error means, it can throw it to catch block.