Here is a listing of C++ programming interview questions on “Catching Exceptions” along with answers, explanations and/or solutions:
1. How many parameters does the throw expression can have?
a) 1
b) 2
c) 3
d) 4
View Answer
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
View Answer
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
View Answer
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
View Answer
Explanation: In this program, We are arising a standard exception and catching that and returning a statement.
Output:
$ g++ goe.cpp $ a.out exception arised
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
View Answer
Explanation: 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
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
View Answer
Explanation: 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!
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
View Answer
Explanation: It will arise a exception because we missed a operator.
Output:
$ g++ goe3.cpp $ a.out is not a valid 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
View Answer
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.
Output:
$ g++ goe4.cpp $ a.out 2.236
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
View Answer
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
View Answer
Explanation: copy-constructor is mandatory for throwing a object.
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.
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check C++ Books
- Apply for Computer Science Internship
- Check Programming Books