C++ Exception Handling MCQ Quiz

Test your knowledge of C++ Exception Handling with this interactive quiz. Select the correct answer for each question and see immediate feedback.

Medium Level Questions Medium

1

Which of the following keywords is used to throw an exception in C++?

Correct Answer: C) throw

The throw keyword is used to raise or throw an exception in C++. The try block identifies a block of code where exceptions can occur, and catch blocks handle the exceptions.

2

What is the output of the following code?
try {
  throw 20;
}
catch (int e) {
  cout << "Exception #" << e;
}
catch (...) {
  cout << "Default exception";
}

Correct Answer: A) Exception #20

The code throws an integer exception with value 20, which is caught by the first catch block that handles integers. The ellipsis catch block (...) is for any exception type but is not reached in this case.

3

Which of the following is the base class for all standard exception classes in C++?

Correct Answer: B) std::exception

std::exception is the base class for all standard exceptions defined in the <exception> header. It provides a virtual member function what() that returns a descriptive string.

4

What happens if an exception is thrown but not caught anywhere in the program?

Correct Answer: B) The program terminates abruptly

If an exception is not caught anywhere in the program, the C++ runtime calls the terminate() function, which by default aborts the program. This can be customized using set_terminate().

5

Which exception class should be used for errors that can only be detected during runtime?

Correct Answer: B) std::runtime_error

std::runtime_error is the base class for exceptions that can only be detected during runtime, such as range errors or arithmetic overflow. std::logic_error is for errors preventable by proper coding.

6

What is the purpose of the catch-all handler catch(...)?

Correct Answer: A) To catch any type of exception

The catch-all handler catch(...) can catch any type of exception thrown. It's often used as a last resort to ensure no exception goes unhandled, but it should be used carefully as it doesn't provide access to the exception object.

7

Which of the following is NOT a standard exception class in C++?

Correct Answer: C) std::division_by_zero

C++ does not have a standard std::division_by_zero exception. Division by zero typically causes undefined behavior rather than throwing an exception. The other options are all standard exception classes.

8

What is the output of the following code?
#include <iostream>
#include <stdexcept>
using namespace std;

int main() {
  try {
    try {
      throw runtime_error("Inner exception");
    }
    catch (runtime_error& e) {
      cout << "Caught: " << e.what() << endl;
      throw; // rethrow
    }
  }
  catch (runtime_error& e) {
    cout << "Rethrown caught: " << e.what();
  }
  return 0;
}

Correct Answer: C) Caught: Inner exception
Rethrown caught: Inner exception

The inner catch block catches the exception, prints "Caught: Inner exception", and then rethrows the same exception using throw; without an operand. The outer catch block then catches the rethrown exception and prints "Rethrown caught: Inner exception".

9

What is the purpose of the noexcept specifier in C++?

Correct Answer: B) To indicate that a function will not throw exceptions

The noexcept specifier indicates that a function is not expected to throw any exceptions. If a function declared noexcept does throw an exception, std::terminate is called. This allows for optimizations and better code documentation.

10

Which function is called when a dynamic_cast fails when casting to a reference type?

Correct Answer: B) std::bad_cast()

When a dynamic_cast fails on a reference type (not pointer type), it throws a std::bad_cast exception. For pointer types, dynamic_cast returns nullptr instead of throwing an exception.

Advanced Level Questions Advanced

11

What is the purpose of the std::uncaught_exceptions() function introduced in C++17?

Correct Answer: A) To determine how many exceptions are currently uncaught

std::uncaught_exceptions() returns the number of exceptions currently being handled (i.e., for which catch blocks have been entered but not exited). This is useful for implementing destructors that behave differently depending on whether they're called during stack unwinding.

12

What is the main difference between exception handling in C++ and Java?

Correct Answer: B) Java has checked exceptions, C++ doesn't

Java has checked exceptions (exceptions that must be either caught or declared in the method signature), while C++ does not have this concept. In C++, any type can be thrown as an exception, not just objects derived from std::exception.

13

What is the problem with throwing exceptions from destructors in C++?

Correct Answer: B) It may lead to program termination if the destructor is called during stack unwinding

If a destructor throws an exception while another exception is already active (i.e., during stack unwinding), the C++ runtime calls std::terminate(), which by default aborts the program. This is why destructors should generally not throw exceptions.

14

Which of the following is a valid way to create a custom exception class in C++?

Correct Answer: D) All of the above

All are valid approaches. The most common practice is to inherit from std::exception or one of its derived classes like std::runtime_error and override the what() method. However, any class with a suitable interface can be used as an exception.

15

What does the std::rethrow_exception function do?

Correct Answer: B) Throws an exception stored in a std::exception_ptr

std::rethrow_exception is used to throw an exception that was previously captured and stored in a std::exception_ptr. This is particularly useful for transferring exceptions between threads or for deferred exception handling.

Your score will appear here