Here is a listing of C++ programming questions on “Uncaught Exceptions” along with answers, explanations and/or solutions:
1. What happens if try catch block is not used?
a) arise an error
b) program will run
c) execute continuously
d) wrong output
View Answer
Explanation: If try catch block is not used the exception thrown by the program will be uncaught hence will result into error(s).
2. Which handler is used to handle all types of exception?
a) catch handler
b) catch-all handler
c) catch-none handler
d) try-catch handler
View Answer
Explanation: To catch all types of exceptions, we use the catch-all handler.
3. Which operator is used as catch-all handler?
a) ellipses operator
b) ternary operator
c) string operator
d) unary operator
View Answer
Explanation: The ellipses operator can be represented as (…).
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Base
{
protected:
int a;
public:
Base()
{
a = 34;
}
Base(int i)
{
a = i;
}
virtual ~Base()
{
if (a < 0) throw a;
}
virtual int getA()
{
if (a < 0)
{
throw a;
}
}
};
int main()
{
try
{
Base b(-25);
cout << endl << b.getA();
}
catch (int)
{
cout << endl << "Illegal initialization";
}
}
a) Illegal initialization
b) Terminate called after throwing an instance of ‘int’
c) Illegal initialization & terminate called after throwing an instance
d) initialization
View Answer
Explanation: As we are throwing a negative number and we are using the only integer, So it is arising an error.
Output:
$ g++ uce.cpp $ a.out terminate called after throwing an instance of 'int'
5. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
using namespace std;
void terminator()
{
cout << "terminate" << endl;
}
void (*old_terminate)() = set_terminate(terminator);
class Botch
{
public:
class Fruit {};
void f()
{
cout << "one" << endl;
throw Fruit();
}
~Botch()
{
throw 'c';
}
};
int main()
{
try
{
Botch b;
b.f();
}
catch(...)
{
cout << "inside catch(...)" << endl;
}
}
a) one
b) inside catch
c)
one terminate
d)
one terminate AbortedView Answer
Explanation: This program uses set_terminate as it is having an uncaught exception.
Output:
$ g++ uce1.cpp $ a.out one terminate Aborted
6. What will be the output of the following C++ code?
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate ()
{
cerr << "terminate handler called";
abort();
}
int main (void)
{
set_terminate (myterminate);
throw 0;
return 0;
}
a) terminate handler called
b) aborted
c) both terminate handler & Aborted
d) runtime error
View Answer
Explanation: In this program, We are using set_terminate to abort the program.
Output:
$ g++ uce2.cpp $ a.out terminate handler called Aborted
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Test1
{
};
class Test2 : public Test1 { };
void Funct();
int main()
{
try
{
Funct();
}
catch (const Test1&)
{
cerr << "Caught a exception" << endl;
}
return 0;
}
void Funct()
{
throw Test2();
}
a) Caught an exception
b) NULL
c) Both Caught an exception & NULL
d) Caught a exception
View Answer
Explanation: In this program, We are arising with the exception by using the method in the class.
Output:
$ g++ uce3.cpp $ a.out Caught a exception
8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
#include <cstdlib>
#include <exception>
void Funct()
{
cout << "Funct() was called by terminate()." << endl;
exit(0);
}
int main()
{
try
{
set_terminate(Funct);
throw "Out of memory!";
}
catch(int)
{
cout << "Integer exception raised." << endl;
}
return 0;
}
a) Integer exception raised
b) Funct() was called by terminate()
c) Integer exception not raised
d) Integer exception raised.
View Answer
Explanation: As there is no integer in this program, We are printing Funct() was called by terminate().
Output:
$ g++ uce4.cpp $ a.out Funct() was called by terminate().
9. What function will be called when we have an uncaught exception?
a) catch
b) throw
c) terminate
d) try
View Answer
Explanation: If we have an uncaught exception means, the compiler will throw the control of the program to terminate function.
10. What will not be called when the terminate() is raised in the constructor?
a) main()
b) class
c) destructor
d) constructor
View Answer
Explanation: To free the memory occupied by that object during initializing and destroy that 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.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Programming MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C++ Books
- Apply for Computer Science Internship