C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”.
Q 1. Where should we place catch block of the derived class in a try-catch block?
A. Before the catch block of Base class
B. After the catch block of Base class
C. Anywhere in the sequence of catch blocks
D. After all the catch blocks
Show Answer
Answer:-A. Before the catch block of Base classExplanation
C++ asks the programmer to place the catch block of derived class before a catch block of the base class, otherwise derived catch block will never be executed.Q 2. What happens when this C++ program is compiled?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
cout<<“Caught A Class\n”;
}
catch(B b){
cout<<“Caught B Class\n”;
}
}
A. The program compiles successfully without any errors or warnings
B. Compile-time error occurs
C. The program compiles successfully with warnings
D. The program gives both errors and warnings
Show Answer
Answer:-C. The program compiles successfully with warningsExplanation
Catch block of derived should always be placed before the catch block base class, hence the program gives warnings stating that exceptions of the derived class will be caught by the base class. Output: $ g++ check.cpp check.cpp: In function ‘int main()’: check.cpp:33:2: warning: exception of type ‘B’ will be caught catch(B b){ ^~~~~ check.cpp:30:2: warning: by earlier handler for ‘A’ catch(A a){ ^~~~~Q 3. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(A a){
cout<<“Caught A Class\n”;
}
catch(B b){
cout<<“Caught B Class\n”;
}
}
A. Caught B Class
B. Caught A Class
C. Compile-time error
D. Run-time error
Show Answer
Answer:-B. Caught A ClassExplanation
As the catch block of the derived class is after the catch block of base class, therefore, all the exceptions of the derived class will be caught by the base class, Hence the output of catch block of class A is printed. Output: $ ./a.out Caught A ClassQ 4. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(B b){
cout<<“Caught B Class\n”;
}
catch(A a){
cout<<“Caught A Class\n”;
}
}
A. Caught B Class
B. Caught A Class
C. Compile-time error
D. Run-time error
Show Answer
Answer:-A. Caught B ClassExplanation
In this as the catch block of the derived class is before the catch block of the base class so when func() throws the object of class B it is caught by the catch block of class B, Hence the output is printed as shown. Output: $ ./a.out Caught B ClassQ 5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func()
{
B b;
throw b;
}
int main()
{
try{
func();
}
catch(B *b){
cout<<“Caught B Class\n”;
}
catch(A a){
cout<<“Caught A Class\n”;
}
}
A. Caught B Class
B. Caught A Class
C. Compile-time error
D. Run-time error
Show Answer
Answer:-B. Caught A ClassExplanation
The func() throws the object of class B but as catch block is defined to catch the exception of class B, Therefore the exception is caught by the base class A. The programmer has defined the catch block for B*, therefore, the object B is not caught by the pointer object B*.Q 6. What id the syntax for catching any type of exceptions?
A. catch(Exception e)
B. catch(…)
C. catch(Exception ALL)
D. catch(ALL)
Show Answer
Answer:-B. catch(…)Explanation
catch(…) is used in C++ to catch all types of exceptions in a single catch block.Q 7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
int a;
public:
A(){}
};
class B: public A
{
int b;
public:
B(){}
};
void func1()
{
B b;
throw b;
}
void func2()
{
A a;
throw a;
}
int main()
{
try{
func1();
}
catch(…){
cout<<“Caught All types of exceptions\n”;
}
try{
func2();
}
catch(B b){
cout<<“Caught All types of exceptions\n”;
}
}
A. Caught All types of exceptions
B. Caught All types of exceptions
Aborted (core dumped)
C. Error
D. Caught All types of exceptions
Caught All types of exceptions
Show Answer
Answer:-B. Caught All types of exceptions Aborted (core dumped)Explanation
Two try-catch blocks is declared each catching the respective exceptions from class A and B. But as we have defined catch all exceptions in the first case, therefore, the exception for class B is caught when thrown by the func1(), but in the second case, the try-catch block is catching only the exception for class B so when func2() throws class A exception and no catch block to catch that exception therefore program results into abort(core dumped).Q 8. Uncaught exception leads to ______________
A. termination of program
B. successful execution of programs
C. no effect on the program
D. execution of other functions of the program starts
Show Answer
Answer:-A. termination of programExplanation
Uncaught exceptions in a program leads to the termination of a program.Q 9. An uncaught handler returns to _______________
A. main function
B. its caller
C. its callee
D. none of the mentioned
Show Answer
Answer:-D. none of the mentionedExplanation
Uncaught exceptions do not “return” to any specific location in the program. They trigger a chain of events leading to program termination. In C++, when an uncaught exception occurs, the program unwinds the stack to find an appropriate exception handler. It searches backward through the call stack (i.e., the series of function calls that led to the point where the exception occurred) until it finds a function that has a suitable exception handler. If no handler is found, the program may terminate or exhibit undefined behavior.Q 10. Header file used for exception handling in C++?
A. <cstdlib>
B. <string>
C. <handler>
D. <exception>
Show Answer
Answer:-D.Explanation
Q 11 .What happens if an exception is thrown but not caught?
A. Program continues
B. std::terminate is called,
C. System crashes,
D. Compilation error
Show Answer
Answer:-B. std::terminate is called,
Q 12.Which keyword is used to specify that a function does not throw exceptions?
A. throw(none),
B. safe
C. noexcept
D. final
Show Answer
Answer:-C. noexcept
Q 13. What is Stack Unwinding?,
A. Cleaning up heap,
B. Destroying local objects
C. Restarting the loop
D. Optimizing code
Show Answer
Answer:-B. Destroying local objects
Q 14. Can a constructor throw an exception?
A. Yes
B. No,
C. Only in C++20
D. Only if static
Show Answer
Answer:-A. Yes
Q 15. What is the base class for all standard C++ exceptions?
A. std::error,
B. std::logic,
C. std::exception,
D. std::base
Show Answer
Answer:-C. std::exception,
Q 16. Which block is executed whether an exception occurs or not?
A. catch
B. finally
C. None (C++ lacks finally)
D. try
Show Answer
Answer:-C. None (C++ lacks finally)
Q 17. How do you re-throw the current exception?
A. throw;
B. throw e;
C. retry;
D. return throw;
Show Answer
Answer:-A. throw;
Q 18. Which exception is thrown by new if memory allocation fails?
A. std::bad_cast
B. std::bad_alloc
C. std::overflow
D. std::mem_fail
Show Answer
Answer:-B. std::bad_alloc
Q 19. What is the risk of throwing an exception in a destructor?
A. Memory leak
B. Memory corruption
C. std::terminate
D. Code bloat
Show Answer
Answer:-C. std::terminateQ 20. Catching an exception by _ is generally preferred to avoid slicing.
A. Value
B.Pointer
C. Reference
D.Static
Show Answer
Answer:-C. ReferenceQ 21.Which header must be included to use standard exceptions?,,,,
Show Answer
Answer:-Q 22. What does catch(…) do?
A. Catches integers
B. Catches all types
C. Catches nothing
D. Syntax error
Show Answer
Answer:-B. Catches all typesQ 23. An exception thrown in a noexcept function results in:
A. A warning
B. std::terminate
C. A silent ignore
D. std::retry
Show Answer
Answer:-B. std::terminateQ 24. Which exception is thrown by dynamic_cast on a failed reference cast?
A. std::bad_type
B. std::bad_alloc
C. std::bad_cast
D. std::nullptr
Show Answer
Answer:-C. std::bad_castQ 25. Can you have multiple catch blocks for a single try?
A. Yes
B. No,
C. Only two,
D. Only if nested
Show Answer
Answer:-A. YesQ 26.Which exception is thrown for mathematical underflow?
A. std::range_error
B. std::underflow_erro
C. std::math_error
D. std::logic_error
Show Answer
Answer:-B. std::underflow_erroQ 27. std::runtime_error is derived from which class?
A. std::logic_error
B. std::exception
C. std::system
D. std::error
Show Answer
Answer:-B. std::exceptionQ 28.What is the purpose of the what() method?
A. Delete exception
B. Get error message
C. Throw exception
D. Identify line number
Show Answer
Answer:-B. Get error messageQ 29. Exception handling is a __ mechanism.
A. Compile-time,
B. Link-time,
C. Run-time
D .Pre-processor
Show Answer
Answer:-C. Run-time
Q 30.”In nested try blocks, where is an exception handled first?
A. Outer catch
B. Inner catch
C. Global handler
D. It is ignored


Leave a Reply