C++ Exception Handling MCQ

Welcome to this quiz on one of the most critical aspects of C++ – Exception Handling. Exception handling allows developers to manage unforeseen errors and provide ways to handle them gracefully without crashing the entire application. Let’s test your knowledge of this essential topic!

1. Which keyword is used to handle an exception?

a) catch
b) throw
c) try
d) handle

Answer:

a) catch

Explanation:

The catch block is used to handle exceptions. It is paired with a try block which contains the code that might throw exceptions.

2. Which keyword is used to signal the occurrence of an exception?

a) signal
b) raise
c) alert
d) throw

Answer:

d) throw

Explanation:

The throw keyword is used to signal or throw an exception.

3. What is the purpose of the try block?

a) To catch the exception
b) To throw the exception
c) To encapsulate potential exception-throwing code
d) None of the above

Answer:

c) To encapsulate potential exception-throwing code

Explanation:

The try block contains the segment of code where an exception might occur.

4. Which of the following can be thrown as an exception in C++?

a) int
b) char
c) an object
d) All of the above

Answer:

d) All of the above

Explanation:

In C++, almost any value or object can be thrown as an exception.

5. If you have multiple catch blocks for a try block, how are they executed?

a) All catch blocks are executed sequentially
b) Only the first catch block is executed
c) Only the catch block matching the thrown exception type is executed
d) Last catch block is executed

Answer:

c) Only the catch block matching the thrown exception type is executed

Explanation:

When an exception is thrown, the catch blocks are examined in order: the first catch block that can handle the exception type is executed.

6. Which standard library class can be used for exception handling in C++?

a) exception
b) stdexcept
c) exceptions
d) error

Answer:

a) exception

Explanation:

The exception class is the standard library class in C++ for exception handling.

7. What will happen if an exception is not caught?

a) The program continues normally
b) The program crashes
c) The exception is ignored
d) The exception is caught by the OS

Answer:

b) The program crashes

Explanation:

If an exception is thrown but not caught, the C++ runtime system handles it by terminating the program.

8. Which keyword is used to specify a block of code that must be executed after the try-catch block?

a) finally
b) lastly
c) conclude
d) None of the above

Answer:

d) None of the above

Explanation:

Unlike some other languages like Java, C++ doesn’t have a finally block.

9. What is the base class for all standard exception classes?

a) error
b) std::exception
c) std::base_exception
d) throw

Answer:

b) std::exception

Explanation:

In C++, std::exception is the base class for all standard exceptions.

10. Which of the following is not a standard exception derived from std::exception?

a) std::bad_alloc
b) std::out_of_range
c) std::throws
d) std::length_error

Answer:

c) std::throws

Explanation:

There isn’t any std::throws exception in the standard C++ library.

Exception handling is a critical aspect of C++, offering developers a way to predictably manage unforeseen errors. Remember, understanding how to utilize exception handling can make your software robust and user-friendly. How did you fare in this quiz? Keep studying and practicing to master all aspects of C++!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top