C++ programming questions and answers focuses on “Error Handling”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ programming questions on “Error Handling” along with answers, explanations and/or solutions:
Q 1. Which keyword is used to handle the expection?
A. try
B. throw
C. catch
D. handler
Show Answer
C. catchExplanation
When we found a exception in the program, We need to throw that and we handle that by using the catch keyword.
Q 2. Which is used to throw a exception?
A. throw
B. try
C. catch
D. handler
Show Answer
A. throwExplanation
throw keyword is used to throw an exception. eg: if(divisor == 0) { throw “Divide by zero error”; }
Q 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
Show Answer
B. It will be executed at the end of the program even if the exception arisedExplanation
finally keyword will be executed at the end of all the exception.Q 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. 0
B. 50
C. Division by zero condition!
D. Error
Show Answer
C. Division by zero condition!Explanation
It’s a mathematical certainty, We can’t divide by zero, So we’re arising a exception. Output: Note: Join free Sanfoundry classes at Telegram or Youtube $ g++ excep.cpp $ a.out Division by zero condition!
Q 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
Show Answer
B. An exception occurredExplanation
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
Q 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. My exception
C. Runtime error
D. Error
Show Answer
B. My exceptionExplanation
This is a standard exception handler used in the class. Output: $ g++ excep2.cpp $ a.out My exception
Q 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
Show Answer
C. Depends on the memoryExplanation
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
Q 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
Show Answer
C. Exception caught C++ ExceptionExplanation
We are defining the user-defined exception in this program. Output: $ g++ excep4.cpp $ a.out C++ Exception Exception caught
Q 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
Show Answer
A. inheriting and overriding exception class functionalityExplanation
User defined exceptions can be done by inheriting and overriding the exception class functionality.
Q 10. Which exception is thrown by dynamic_cast?
A. bad_typeid
B. bad_cast
C. bad_exception
D. bad_alloc
Leave a Reply