1. What is exception handling in C++?
Correct Answer: a) A mechanism to respond to exceptional conditions
Explanation:
Exception handling separates error response code from normal control flow.
2. Which keyword begins a protected block that can catch exceptions?
Correct Answer: b) try
Explanation:
A try block encloses code that may throw exceptions.
3. Which keyword defines a handler for an exception?
Correct Answer: c) catch
Explanation:
catch blocks handle exceptions of matching types.
4. Which keyword raises an exception?
Correct Answer: d) throw
Explanation:
throw transfers control to an exception handler.
5. What does a throw expression do?
Correct Answer: a) Transfers control to a matching handler
Explanation:
throw begins exception propagation.
6. What happens if no catch block matches an exception?
Correct Answer: b) The exception propagates to the caller
Explanation:
Uncaught exceptions move up the call stack.
7. What happens if no handler handles an exception?
Correct Answer: c) std::terminate is called
Explanation:
Unhandled exceptions cause termination by default.
8. Can an int be thrown as an exception in C++?
Correct Answer: d) Yes
Explanation:
C++ allows almost any type to be thrown.
9. What does catch(...) do?
Correct Answer: a) Catches any exception
Explanation:
The ellipsis handler matches all exception types.
10. In what order should catch blocks usually be written?
Correct Answer: b) Most derived classes first
Explanation:
Catch derived types before base types to avoid hiding them.
11. What is an exception object?
Correct Answer: c) The object thrown and caught
Explanation:
The thrown value is stored during stack unwinding.
12. What is a common best practice for throwing and catching exceptions?
Correct Answer: d) Throw by value and catch by reference
Explanation:
This avoids slicing and lifetime problems.
13. What is stack unwinding?
Correct Answer: a) Destroying local objects while an exception propagates
Explanation:
Destructors for constructed local objects run during unwinding.
14. What does RAII help with during exceptions?
Correct Answer: b) Automatic resource cleanup
Explanation:
RAII releases resources in destructors during unwinding.
15. What does std::exception provide?
Correct Answer: c) A base class with what()
Explanation:
Standard exceptions derive from std::exception.
16. What does what() return?
Correct Answer: d) A const char pointer message
Explanation:
what provides implementation-defined error text.
17. What is std::runtime_error?
Correct Answer: a) A class derived from std::exception
Explanation:
runtime_error represents errors detected at runtime.
18. What is std::logic_error?
Correct Answer: b) A class derived from std::exception
Explanation:
logic_error represents errors in program logic.
19. What is std::invalid_argument derived from?
Correct Answer: c) std::logic_error
Explanation:
invalid_argument reports invalid function arguments.
20. What is std::out_of_range derived from?
Correct Answer: d) std::logic_error
Explanation:
out_of_range reports arguments outside valid ranges.
21. Which exception is commonly thrown by new when allocation fails?
Correct Answer: a) std::bad_alloc
Explanation:
new throws bad_alloc when memory cannot be allocated.
22. What is std::overflow_error derived from?
Correct Answer: b) std::runtime_error
Explanation:
overflow_error reports arithmetic overflow.
23. What is std::underflow_error derived from?
Correct Answer: c) std::runtime_error
Explanation:
underflow_error reports arithmetic underflow.
24. What is std::length_error derived from?
Correct Answer: d) std::logic_error
Explanation:
length_error reports attempts to exceed maximum size.
25. Can a throw expression create a temporary exception object?
Correct Answer: a) Yes
Explanation:
throw can construct a temporary object.
26. Which catch parameter is usually recommended for standard exceptions?
Correct Answer: b) catch (const std::exception& e)
Explanation:
Const reference avoids slicing and allows polymorphism.
27. What can happen if you catch a polymorphic exception by value?
Correct Answer: c) Object slicing
Explanation:
Catching by value can copy only the base part.
28. What is true about catch (std::exception e)?
Correct Answer: d) It can cause object slicing
Explanation:
Derived exception data may be lost.
29. How can you rethrow the current exception inside a catch block?
Correct Answer: a) throw;
Explanation:
A bare throw rethrows the current exception.
30. What happens if throw; is executed outside a catch block?
Correct Answer: b) It calls std::terminate
Explanation:
There is no current exception to rethrow.
31. Can constructors throw exceptions?
Correct Answer: c) Yes
Explanation:
Constructors can report initialization failure by throwing.
32. What happens if a constructor throws an exception?
Correct Answer: d) Fully constructed subobjects are destroyed
Explanation:
The object itself was never fully constructed.
33. Should destructors usually throw exceptions?
Correct Answer: a) No
Explanation:
Throwing destructors can cause termination during unwinding.
34. What can happen if a destructor throws during stack unwinding?
Correct Answer: b) It may cause std::terminate
Explanation:
Multiple active exceptions are unsafe.
35. What does noexcept mean?
Correct Answer: c) The function is not expected to throw
Explanation:
noexcept supports optimization and safety contracts.
36. What happens if an exception leaves a noexcept function?
Correct Answer: d) std::terminate is called
Explanation:
Violating noexcept ends the program by default.
37. What does the noexcept operator return?
Correct Answer: a) A Boolean
Explanation:
It checks whether an expression is declared noexcept.
38. What does noexcept(false) indicate?
Correct Answer: b) It indicates the function may throw
Explanation:
noexcept(false) is the absence of a no-throw guarantee.
39. Are destructors noexcept by default in modern C++?
Correct Answer: c) Yes
Explanation:
Destructors are implicitly noexcept unless specified otherwise.
40. Can move constructors be noexcept?
Correct Answer: d) Yes, and it can improve container behavior
Explanation:
Containers may prefer noexcept move operations.
41. When is std::terminate commonly called?
Correct Answer: a) Unhandled exceptions or noexcept violations
Explanation:
terminate ends abnormal exception situations.
42. Which function can replace the terminate handler?
Correct Answer: b) std::set_terminate
Explanation:
set_terminate installs a custom termination handler.
43. What is exception safety?
Correct Answer: c) Keeping objects valid and resources safe when exceptions occur
Explanation:
Exception-safe code maintains invariants during failures.
44. Which guarantee means no changes occur if an exception is thrown?
Correct Answer: d) Strong exception guarantee
Explanation:
Operations succeed completely or have no effect.
45. Which guarantee says objects remain valid after an exception?
Correct Answer: a) Basic exception guarantee
Explanation:
Resources are not leaked and invariants remain valid.
46. Which guarantee means a function never throws?
Correct Answer: b) Nothrow guarantee
Explanation:
Nothrow operations must not throw exceptions.
47. Which idiom is commonly used for exception-safe assignment?
Correct Answer: c) Copy-and-swap
Explanation:
It provides strong safety by working on a copy.
48. What is a scope guard?
Correct Answer: d) An RAII cleanup helper
Explanation:
Scope guards run cleanup when scope exits.
49. What is the purpose of a try block?
Correct Answer: a) To enclose code that may throw
Explanation:
try groups code with associated handlers.
50. Can a try block execute normally without throwing?
Correct Answer: b) Yes
Explanation:
try blocks do not require an exception to occur.
51. Can a catch block throw another exception?
Correct Answer: c) Yes
Explanation:
Handlers can translate or wrap exceptions.
52. Can a catch block finish by returning normally?
Correct Answer: d) Yes
Explanation:
A handler can recover and continue execution.
53. What did an empty dynamic exception specification throw() mean in older C++?
Correct Answer: a) The function should not throw exceptions
Explanation:
throw() was an early no-exception specification.
54. What happened to dynamic exception specifications in C++11?
Correct Answer: b) They were deprecated
Explanation:
Modern C++ prefers noexcept.
55. What happened to dynamic exception specifications in C++17?
Correct Answer: c) They were removed, but throw() remained as deprecated noexcept
Explanation:
Dynamic specifications were removed from the language.
56. Can nullptr be thrown?
Correct Answer: d) Yes
Explanation:
Pointer values can be thrown.
57. Can an enum value be thrown?
Correct Answer: a) Yes
Explanation:
Enums are valid exception types.
58. Can a char be caught?
Correct Answer: b) Yes
Explanation:
Any thrown type can have a matching catch.
59. Can a function have multiple catch blocks?
Correct Answer: c) Yes
Explanation:
Multiple handlers can handle different exception types.
60. What is true about catch(...)?
Correct Answer: d) It catches without naming the exception object
Explanation:
The ellipsis handler does not provide a parameter name.
61. Which function captures the current exception into an exception_ptr?
Correct Answer: a) std::current_exception
Explanation:
It stores an exception for later rethrowing.
62. What can std::exception_ptr store?
Correct Answer: b) A stored exception
Explanation:
exception_ptr can hold an exception across scopes.
63. Which function rethrows a stored exception_ptr?
Correct Answer: c) std::rethrow_exception
Explanation:
It throws the exception stored in an exception_ptr.
64. Which function creates an exception_ptr from an exception object?
Correct Answer: d) std::make_exception_ptr
Explanation:
It constructs and stores an exception object.
65. How can exceptions be transferred between threads?
Correct Answer: a) Using std::exception_ptr
Explanation:
exception_ptr can be passed across threads.
66. Which function returns the number of active uncaught exceptions?
Correct Answer: b) std::uncaught_exceptions
Explanation:
It returns the active exception count.
67. What is true about std::uncaught_exception without the final s?
Correct Answer: c) It is deprecated
Explanation:
std::uncaught_exceptions is the modern alternative.
68. Should exceptions be used for normal control flow?
Correct Answer: d) Generally discouraged
Explanation:
Exceptions are best for exceptional conditions.
69. Which is an alternative to exceptions for error handling?
Correct Answer: a) Return codes, optional, or expected-like types
Explanation:
Not all errors require exceptions.
70. Which type can represent a value that may be absent?
Correct Answer: b) std::optional
Explanation:
optional can model missing values without exceptions.
71. Which C++23 type can represent a value or an error?
Correct Answer: c) std::expected
Explanation:
expected stores either a result or an error.
72. Which C-compatible error indicator is often used with system functions?
Correct Answer: d) errno
Explanation:
errno stores error codes for many C APIs.
73. Which macro checks a runtime assumption and may abort?
Correct Answer: a) assert
Explanation:
assert tests conditions during debugging.
74. Which construct checks conditions at compile time?
Correct Answer: b) static_assert
Explanation:
static_assert fails compilation if false.
75. Which macro can disable assert checks?
Correct Answer: c) NDEBUG
Explanation:
NDEBUG removes assert checks.
76. What must static_assert use?
Correct Answer: d) A constant expression
Explanation:
Compile-time checks require constant expressions.
77. Can allocation failure cause exceptions in C++?
Correct Answer: a) Yes, through std::bad_alloc
Explanation:
new reports allocation failure with exceptions by default.
78. Which form of new returns nullptr instead of throwing?
Correct Answer: b) new (std::nothrow)
Explanation:
nothrow new returns nullptr on failure.
79. Does malloc throw exceptions?
Correct Answer: c) No, it returns nullptr on failure
Explanation:
malloc is a C allocation function.
80. How should allocation failures be handled in C++?
Correct Answer: d) Handle exceptions or use nothrow depending on design
Explanation:
Choose error handling that matches system requirements.
81. Which exception can dynamic_cast throw for references?
Correct Answer: a) std::bad_cast
Explanation:
Reference dynamic_cast throws on failure.
82. What does dynamic_cast return on pointer failure?
Correct Answer: b) nullptr
Explanation:
Pointer dynamic_cast returns nullptr when it fails.
83. Which exception can typeid throw for a null polymorphic pointer?
Correct Answer: c) std::bad_typeid
Explanation:
typeid on a null polymorphic pointer can throw.
84. Which exception may be thrown by calling an empty std::function?
Correct Answer: d) std::bad_function_call
Explanation:
Calling a targetless function object throws.
85. Which class wraps operating-system error information?
Correct Answer: a) std::system_error
Explanation:
system_error carries an error_code.
86. Which type stores an error value and category?
Correct Answer: b) std::error_code
Explanation:
error_code represents system-specific errors.
87. Which type provides a portable error condition?
Correct Answer: c) std::error_condition
Explanation:
error_condition abstracts error categories.
88. Which enumeration defines portable error conditions?
Correct Answer: d) std::errc
Explanation:
errc lists common error conditions.
89. Which exception can vector::at throw?
Correct Answer: a) std::out_of_range
Explanation:
at performs bounds checking.
90. Which exception can map::at throw when a key is missing?
Correct Answer: b) std::out_of_range
Explanation:
map::at reports missing keys.
91. Which exception can std::stoi throw if conversion fails?
Correct Answer: c) std::invalid_argument
Explanation:
stoi throws when no conversion can be performed.
92. Which exception can std::stoi throw if the value is too large?
Correct Answer: d) std::out_of_range
Explanation:
stoi throws when the number is out of int range.
93. Which exception can std::string::substr throw for a bad position?
Correct Answer: a) std::out_of_range
Explanation:
substr checks the starting position.
94. Which exception can std::string::at throw for a bad index?
Correct Answer: b) std::out_of_range
Explanation:
at checks character indexes.
95. How should exceptions usually be caught?
Correct Answer: c) By const reference
Explanation:
Const reference preserves type and avoids slicing.
96. What should custom exception classes usually derive from?
Correct Answer: d) std::exception
Explanation:
Deriving from std::exception integrates with handlers.
97. What should a custom exception class usually override?
Correct Answer: a) what()
Explanation:
what provides the error message.
98. What is a good property of exception objects?
Correct Answer: b) Immutable
Explanation:
Exceptions should carry stable error information.
99. What does an exception hierarchy allow?
Correct Answer: c) Catch broad or specific handlers
Explanation:
Hierarchies support flexible error handling.
100. What is a best practice for C++ error handling?
Correct Answer: d) Use exceptions for exceptional errors with RAII
Explanation:
RAII and exception safety keep programs robust.










