C++ Programming Questions and Answers – Catching Exceptions

C++ programming interview questions and answers focuses on “Catching Exceptions”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ programming interview questions on “Catching Exceptions” along with answers, explanations and/or solutions:

Q 1. How many parameters does the throw expression can have?
A. 1
B. 2
C. 3
D. 4

Show Answer A. 1
Explanation In c++ program, We can be able to throw only one error at a time.

Q 2. Where exception are handled?
A. inside the program
B. main program
C. outside the regular code
D. both inside or outside

Show Answer C. outside the regular code
Explanation Exception are handled outside the regular code.

Q 3. Which is used to check the error in the block?
A. try
B. throw
C. catch
D. handler

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

Q 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. runtime error
C. exception
D. error

Show Answer A. exception arised
Explanation In this program, We are arising a standard exception and catching that and returning a statement. Output: Note: Join free Sanfoundry classes at Telegram or Youtube $ g++ goe.cpp $ a.out exception arised

Q 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

Show Answer A. 5
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

Q 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. 100
D. Division by zero condition!

Show Answer D. Division by zero condition!
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!

Q 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

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

Q 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. Cannot take sqrt of negative number
D. Error

Show Answer B. 2.236
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


Q 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

Show Answer A. We have to throw an exception
Explanation As a constructor don’t have a return type, We have to throw the exception.


Q 10. What should present when throwing a object?
A. constructor
B. copy-constructor
C. destructor
D. copy-destructor

Show Answer B. copy-constructor
Explanation copy-constructor is mandatory for throwing a object.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page