Here is a listing of C++ programming questions on “Error Handling” along with answers, explanations and/or solutions:
1. Which keyword is used to handle the expection?
a) try
b) throw
c) catch
d) handler
View Answer
Explanation: When we found a exception in the program, We need to throw that and we handle that by using the catch keyword.
2. Which is used to throw a exception?
a) throw
b) try
c) catch
d) handler
View Answer
Explanation: throw keyword is used to throw an exception.
eg:
if(divisor == 0) { throw "Divide by zero error"; }
3. What is the use of the ‘finally’ keyword?
a) It used to execute at the starting of the program
b) It will be executed at the end of the program even if the exception arised
c) It will be executed at the starting of the program even if the exception arised
d) It will be executed at the middle of the program even if the exception arised
View Answer
Explanation: finally keyword will be executed at the end of all the exception.
4. 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)
{
cerr << msg << endl;
}
return 0;
}
a) 50
b) 0
c) Division by zero condition!
d) Error
View Answer
Explanation: It’s a mathematical certainty, We can’t divide by zero, So we’re arising a exception.
Output:
$ g++ excep.cpp $ a.out Division by zero condition!
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred " << e << endl;
}
return 0;
}
a) 20
b) An exception occurred
c) Error
d) An exception occurred 20
View Answer
Explanation: We are handling the exception by throwing that number. So the output is printed with the given number.
Output:
$ g++ excep1.cpp $ a.out An exception occurred 20
6. 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 "My exception";
}
} myex;
int main ()
{
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
a) Exception
b) Error
c) My exception
d) Runtime error
View Answer
Explanation: This is a standard exception handler used in the class.
Output:
$ g++ excep2.cpp $ a.out My exception
7. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
int main ()
{
try
{
int* myarray = new int[1000];
cout << "allocated";
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}
a) Allocated
b) Standard exception
c) Depends on the memory
d) Error
View Answer
Explanation: In this program, We are allocating the memory for array. If it is allocated means, no exception will arise and if there is no size in memory means, Exception will arise.
Output:
$ g++ excep3.cpp $ a.out allocated
8. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception
{
const char * what () const throw ()
{
return "C++ Exception";
}
};
int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
cout << "Exception caught" << std::endl;
cout << e.what() << std::endl;
}
catch(std::exception& e)
{
}
}
a) C++ Exception
b) Exception caught
c)
Exception caught C++ Exception
d) Error
View Answer
Explanation: We are defining the user-defined exception in this program.
Output:
$ g++ excep4.cpp $ a.out C++ Exception Exception caught
9. How do define the user-defined exceptions?
a) inheriting and overriding exception class functionality
b) overriding class functionality
c) inheriting class functionality
d) delting and adding class member
View Answer
Explanation: User defined exceptions can be done by inheriting and overriding the exception class functionality.
10. Which exception is thrown by dynamic_cast?
a) bad_cast
b) bad_typeid
c) bad_exception
d) bad_alloc
View Answer
Explanation: bad_cast exception is thrown by dynamic_cast.
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.
- Check Programming Books
- Apply for C++ Internship
- Check Computer Science Books
- Apply for Computer Science Internship
- Check C++ Books