1. How many parameters does the throw expression can have?
A. 1
B. 2
C. 3
D. 4
Answer: A
Explanation:
In c++ program, We can be able to throw only one error at a time.
2. Where exception are handled?
A. inside the program
B. outside the regular code
C. both inside or outside
D. main program
Answer: B
Explanation:
Exception are handled outside the regular code.
3. Which is used to check the error in the block?
A. try
B. throw
C. catch
D. handler
Answer: A
Explanation:
The try block is used to check for errors, if there is any error means, it can throw it to catch block.
4. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
class myexception: public exception
{
virtual const char* what() const throw()
{
return "exception arised";
}
} myex;
int main ()
{
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
A. exception arised
B. error
C. exception
D. runtime error
Answer: A
Explanation:
In this program, We are arising a standard exception and catching that and returning a statement.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int age=5;
try
{
if (age < 0)
throw "Positive Number Required";
cout << age << "\n\n";
}
catch(const char* Message)
{
cout << "Error: " << Message;
}
return 0;
}
A. 5
B. 10
C. 15
D. Positive Number Required
Answer: A
Explanation:
In this program, We are checking the age of a person, If it is zero means, We will arise a exception.
6. 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 char* msg)
{
cout << msg << endl;
}
return 0;
}
A. 50
B. 0
C. Division by zero condition!
D. 100
Answer: C
Explanation:
We are dividing the values and if one of the values is zero means, We are arising an exception.
7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main()
{
double Op1 = 10, Op2 = 5, Res;
char Op;
try
{
if (Op != '+' && Op != '-' && Op != '*' && Op != '/')
throw Op;
switch(Op)
{
case '+':
Res = Op1 + Op2;
break;
case '-':
Res = Op1 - Op2;
break;
case '*':
Res = Op1 * Op2;
break;
case '/':
Res = Op1 / Op2;
break;
}
cout << "\n" << Op1 << " " << Op << " "<< Op2 << " = " << Res;
}
catch (const char n)
{
cout << n << " is not a valid operator";
}
return 0;
}
A. 15
B. 5
C. 2
D. is not a valid operator
Answer: D
Explanation:
It will arise a exception because we missed a operator.
8. What will be the output of the following C++ code?
#include<iostream>
#include "math.h"
using namespace std;
double MySqrt(double D.
{
if (d < 0.0)
throw "Cannot take sqrt of negative number";
return sqrt(D.;
}
int main()
{
double d = 5;
cout << MySqrt(D. << endl;
}
A. 5
B. 2.236
C. Error
D. Cannot take sqrt of negative number
Answer: B
Explanation:
We are finding the square root of the number, if it is a positive number, it can manipulate, Otherwise it will arise a exception.
9. How to handle the exception in constructor?
A. We have to throw an exception
B. We have to return the exception
C. We have to throw an exception & return the exception
D. We have to catch an exception
Answer: A
Explanation:
As a constructor don’t have a return type, We have to throw the exception.
10. What should present when throwing a object?
A. constructor
B. copy-constructor
C. destructor
D. copy-destructor
Answer: B
Explanation:
copy-constructor is mandatory for throwing a object.