C++ programming questions and answers focuses on “Uncaught Exceptions”. 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. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ programming questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ programming questions on “Uncaught Exceptions” along with answers, explanations and/or solutions:
Q 1. What happens if try catch block is not used?
A. program will run
B. arise an error
C. execute continuously
D. wrong output
Show Answer
B. arise an errorExplanation
If try catch block is not used the exception thrown by the program will be uncaught hence will result into error(s).
Q 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
Show Answer
B. catch-all handlerExplTo catch all types of exceptions, we use the catch-all handler.anation
Q 3. Which operator is used as catch-all handler?
A. ellipses operator
B. ternary operator
C. string operator
D. unary operator
Show Answer
A. ellipses operatorExplanation
The ellipses operator can be represented as (…).Q 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
Show Answer
B. Terminate called after throwing an instance of ‘int’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’
Q 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
Aborted
Show Answer
D. one terminate AbortedExplanation
This program uses set_terminate as it is having an uncaught exception. Output: $ g++ uce1.cpp $ a.out one terminate AbortedQ 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. runtime error
D. both terminate handler & Aborted
Show Answer
D. both terminate handler & AbortedExplanation
In this program, We are using set_terminate to abort the program. Output: $ g++ uce2.cpp $ a.out terminate handler called Aborted
Q 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
Show Answer
A. Caught an exceptionExplanation
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
Q 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.
Show Answer
B. Funct() was called by terminate()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().
Q 9. What function will be called when we have an uncaught exception?
A. catch
B. throw
C. terminate
D. try
Show Answer
C. terminateExplanation
If we have an uncaught exception means, the compiler will throw the control of the program to terminate function.
Q 10. What will not be called when the terminate() is raised in the constructor?
A. main()
B. class
C. destructor
D. constructor
Show Answer
C. destructorExplanation
To free the memory occupied by that object during initializing and destroy that object.Q 11. What happens when an exception is thrown but not caught in a C++ program?
A. The program continues to execute normally.
B. The program prints a warning and then terminates.
C. The program terminates abruptly, and an error message is typically displayed by the operating system.
D. The program enters an infinite loop.
Show Answer
C. The program terminates abruptly, and an error message is typically displayed by the operating system.Explanation
The program terminates abruptly, and an error message is typically displayed by the operating system. When an exception is not handled by any catch block, the C++ runtime calls std::terminate(), which by default calls std::abort(), leading to program termination.Q 12. Which function is called by the C++ runtime when an uncaught exception is detected?
A. std::uncaught_exception()
B. std::abort()
C. std::terminate()
D std::handle_uncaught()
Show Answer
C. std::terminate()Explanation
std::terminate() is called when an exception is thrown but not caught by a catch block.Q 13. What is the purpose of the std::terminate() function?
A. To safely exit the program.
B. To throw a new exception.
C. To call the destructor for all local objects.
D. To handle an uncaught exception, which by default terminates the program.
Show Answer
D. To handle an uncaught exception, which by default terminates the program.Explanation
The std::terminate() function is part of the C++ standard library’s exception handling mechanism. It is responsible for handling uncaught exceptions, and its default behavior is to terminate the program by calling std::abort().Q 14. How can you customize the behavior of std::terminate()?
A. By writing a custom catch block.
B. By calling std::set_terminate() to register a new termination handler.
C. It cannot be customized.
D. By throwing a specific exception type.
Show Answer
B. By calling std::set_terminate() to register a new termination handler.Explanation
You can use std::set_terminate() to specify a custom function that will be called instead of the default std::terminate() handler.Q 15. Which of the following is an example of an uncaught exception?
A. A try block with a corresponding catch block.
B. A try block without a corresponding catch block.
C. A function with a noexcept specifier that throws an exception.
D. Both b and c.
Show Answer
D. Both b and c.Explanation
An uncaught exception can occur if there’s no catch block to handle a thrown exception, or if an exception is thrown from a function that is declared noexcept, as the noexcept specifier guarantees that the function will not throw an exception. If an exception is thrown, it’s considered a violation of the guarantee, which leads to std::terminate() being called.Q 16. What is the purpose of the noexcept keyword?
A. It prevents a function from being called.
B. It guarantees that a function will not throw an exception.
C. It catches all exceptions.
D. It throws a new exception.
Show Answer
B. It guarantees that a function will not throw an exception.Explanation
The noexcept keyword is a language feature in C++ that declares a promise by the programmer that the function will not throw any exceptions.Q 17. What happens if a function declared with noexcept throws an exception?
A. The exception is handled normally.
B. The program continues to run without issue.
C. std::terminate() is called, and the program is terminated.
D. The noexcept keyword is ignored.
Show Answer
C. std::terminate() is called, and the program is terminated.Explanation
If a function declared with noexcept throws an exception, the C++ runtime immediately calls std::terminate() to terminate the program. This is because the noexcept promise was broken.Q 18. When is it appropriate to use the noexcept specifier?
A. On all functions.
B. On functions that perform resource cleanup.
C. On functions that are critical to the program’s logic and must not fail.
D. On functions that are known not to throw exceptions.
Show Answer
D. On functions that are known not to throw exceptions.Explanation
The noexcept specifier should be used on functions that you are certain will not throw an exception. This can allow for performance optimizations by the compiler and is useful for functions that perform critical tasks where a failure to throw an exception is a guarantee.Q 19. What is the default behavior of std::terminate() in most C++ implementations?
A. To call std::exit(0).
B. To call std::abort().
C. To throw a std::runtime_error.
D. To print a message and then return.
Show Answer
B. To call std::abort().Explanation
By default, std::terminate() calls std::abort(), which causes the program to terminate immediately and often generates a core dump file for debugging.Q 20. What is the difference between std::terminate() and std::abort()?
A. std::terminate() handles exceptions, while std::abort() doesn’t.
B. std::terminate() is a C++ library function, while std::abort() is a C library function.
C. std::terminate() is called for uncaught exceptions, while std::abort() is a more general-purpose termination function that can be called for various reasons, including by std::terminate().
D. There is no difference.
Show Answer
C. std::terminate() is called for uncaught exceptions, while std::abort() is a more general-purpose termination function that can be called for various reasons, including by std::terminate().Explanation
std::terminate() is specifically designed to handle uncaught exceptions within the C++ runtime. std::abort() is a more fundamental C function that causes abnormal program termination. std::terminate() uses std::abort() as its default behavior.Q 21. Which of the following scenarios will cause std::terminate() to be called?
A. An exception is thrown from a destructor during stack unwinding.
B. An exception is thrown from a global constructor.
C. An exception is thrown from a function with no try-catch block.
D. All of the above.
Show Answer
D. All of the above.Explanation
All these scenarios can lead to std::terminate() being called. Throwing an exception from a destructor during stack unwinding is a critical error, as it would cause two exceptions to be active at the same time, which is not allowed. Throwing an exception from a global constructor is also an issue as it occurs before the main function is even called. An exception thrown from a function without a try-catch block will be an uncaught exception.Q 22.What does the C++ Standard guarantee about the state of the program after an uncaught exception?
A. The program will be in a clean state.
B. The program will be in a consistent state.
C. The state of the program is undefined after an uncaught exception.
D. The program will continue to execute.
Show Answer
C. The state of the program is undefined after an uncaught exception.Explanation
The C++ Standard does not guarantee anything about the program’s state after an uncaught exception. It is considered an abnormal termination.Q 23. Which of the following is a good practice to avoid uncaught exceptions?
A. Use try-catch blocks everywhere.
B. Declare all functions noexcept.
C. Use try-catch blocks at key points in the program, such as the main function or thread entry points, to catch and log any unexpected exceptions.
D. Don’t use exceptions at all.
Show Answer
C. Use try-catch blocks at key points in the program, such as the main function or thread entry points, to catch and log any unexpected exceptions.Explanation
Using try-catch blocks at strategic points, like the main function, ensures that any unexpected exceptions are caught and handled gracefully, rather than causing a sudden program crash.Q 24. What is “stack unwinding” in the context of exceptions?
A. The process of calling std::terminate().
B. The process of calling std::abort().
C. The process of destroying local variables in the functions that were on the call stack between the throw point and the catch point.
D. The process of calling all destructors in the program.
Show Answer
C. The process of destroying local variables in the functions that were on the call stack between the throw point and the catch point.Explanation
Stack unwinding is the mechanism that automatically calls the destructors of local objects in the functions that are exited due to an exception being thrown. This is crucial for Resource Acquisition Is Initialization (RAII) to ensure that resources like memory and file handles are properly released.Q 25. What happens if a destructor called during stack unwinding throws an exception?
A. The exception is handled normally.
B. The two exceptions are handled sequentially.
C. The program terminates immediately via std::terminate() because having two exceptions active simultaneously is an unrecoverable error.
D. The first exception is ignored.
Show Answer
C. The program terminates immediately via std::terminate() because having two exceptions active simultaneously is an unrecoverable error.Explanation
Having two active exceptions is a violation of the C++ exception handling model. If a destructor called during stack unwinding throws an exception, std::terminate() is called immediately, and the program terminates.Q 26. How can you ensure that a C++ program handles all possible exceptions gracefully?
A. By wrapping the entire main function’s body in a try-catch block.
B. By declaring all functions noexcept.
C. By using a global try-catch block.
D. By not using exceptions at all.
Show Answer
A. By wrapping the entire main function’s body in a try-catch block.Explanation
Wrapping the main function in a try-catch block is a common and effective pattern for catching any uncaught exceptions that might propagate up to the top level of the program.Q 27. What is a “re-throw” of an exception?
A. Throwing the same exception object again from a catch block.
B. Throwing a new exception from a catch block.
C. Catching an exception and then letting it propagate.
D. It is not a valid operation in C++.
Show Answer
A. Throwing the same exception object again from a catch block.Explanation
A re-throw is performed by simply using the throw; statement inside a catch block. This allows an exception to be partially handled and then re-thrown to a higher-level handler for further processing.Q 28. Which of the following is true about uncaught exceptions in multi-threaded C++ applications?
A. An uncaught exception in one thread terminates only that thread.
B. An uncaught exception in one thread terminates the entire program.
C. Uncaught exceptions are handled by the main thread.
D. The behavior is undefined.
Show Answer
B. An uncaught exception in one thread terminates the entire program.Explanation
By default, an uncaught exception in any thread of a C++ program will cause std::terminate() to be called, which will terminate the entire program, not just the thread.Q 29. What is a “stack trace”?
A. A list of all global variables.
B. A snapshot of the call stack at the time an event (like an exception) occurs, showing the sequence of function calls.
C. A list of all threads in a program.
D. A list of all available memory.
Show Answer
B. A snapshot of the call stack at the time an event (like an exception) occurs, showing the sequence of function calls.Explanation
A stack trace is a valuable debugging tool that provides a record of the active subroutines (functions) at a particular point in time. When an uncaught exception occurs, a stack trace is often generated to help pinpoint where the exception was thrown.Q 30. Which of the following is the most effective way to prevent a program from terminating due to an uncaught exception?
A. By using std::set_terminate() to replace the default termination handler.
B. By using try...catch blocks at appropriate levels of the program’s hierarchy. C. By using the noexcept keyword on all functions.
D. By simply ignoring the exception.


Leave a Reply