Exception and Event Handling in C ++ Programming MCQ Questions and Answers
1. Which keyword in C++ is used to handle exceptions?
A) error
B) catch
C) handle
D) try
Answer: D) try
2. What is the purpose of the catch block in C++?
A) To raise an exception
B) To declare exceptions
C) To handle the exception thrown by try block
D) To ignore the error
Answer: C) To handle the exception thrown by try block
3. Which of the following is the correct syntax for handling exceptions in C++?
A) try-catch-finally
B) try-catch
C) catch-throw-try
D) try-handle
Answer: B) try-catch
4. Which keyword is used to manually raise an exception?
A) error
B) raise
C) throw
D) signal
Answer: C) throw
5. Which block must immediately follow a try block?
A) throw
B) finally
C) catch
D) handle
Answer: C) catch
6. What happens if an exception is thrown but not caught in C++?
A) Program continues execution
B) It is ignored
C) Program terminates abnormally
D) Compiler catches it automatically
Answer: C) Program terminates abnormally
7. Which type of exceptions are handled by the catch-all handler?
A) Specific exception types
B) All exception types
C) Only runtime errors
D) Syntax errors
Answer: B) All exception types
8. What is the syntax for a catch-all handler?
A) catch(…)
B) catch(…)
C) catch(all)
D) catch(*)
Answer: B) catch(…)
9. Which of the following can be thrown as an exception in C++?
A) Only integers
B) Only classes
C) Only strings
D) Any data type
Answer: D) Any data type
10. What does the throw; statement inside a catch block do?
A) Terminates program
B) Ignores exception
C) Re-throws the caught exception
D) Returns 0
Answer: C) Re-throws the caught exception
11. Which of these is the correct order of exception handling?
A) catch-all first, then specific
B) Specific catch first, then catch-all
C) Random order
D) It does not matter
Answer: B) Specific catch first, then catch-all
12. Which of the following cannot be detected by exception handling?
A) Divide by zero
B) Array index out of bounds
C) Syntax errors
D) Memory allocation failure
Answer: C) Syntax errors
13. Which of the following standard exception classes is defined in <stdexcept>?
A) ios_error
B) runtime_error
C) syntax_error
D) logical_error
Answer: B) runtime_error
14. What is the base class for all standard exceptions in C++?
A) runtime_error
B) error_handler
C) exception_base
D) exception
Answer: D) exception
15. Which method in the exception class returns the description of the error?
A) message()
B) error()
C) what()
D) reason()
Answer: C) what()
16. Which of the following statements about multiple catch blocks is true?
A) They can handle different types of exceptions separately
B) They can only handle one exception
C) Only one catch is allowed
D) Catch blocks cannot have parameters
Answer: A) They can handle different types of exceptions separately
17. Which of the following can be used to define user-defined exceptions?
A) Classes
B) Enums
C) Strings
D) Functions
Answer: A) Classes
18. What is the use of the noexcept specifier in C++?
A) Marks code as safe
B) Declares function cannot be called
C) Indicates that a function does not throw exceptions
D) Forces a catch block
Answer: C) Indicates that a function does not throw exceptions
19. When is the destructor of an object called during an exception?
A) After program termination
B) When the object goes out of scope during stack unwinding
C) Only if no exception occurs
D) Never
Answer: B) When the object goes out of scope during stack unwinding
20. What is stack unwinding?
A) Stack creation
B) Destruction of objects as control leaves scopes due to exception
C) Stack overflow handling
D) Memory allocation
Answer: B) Destruction of objects as control leaves scopes due to exception
21. What will happen if a constructor throws an exception?
A) Destructor of already created objects is called
B) Destructor is skipped
C) Program crashes
D) Nothing happens
Answer: A) Destructor of already created objects is called
22. Can constructors and destructors both throw exceptions in C++?
A) Constructor can throw, destructor should not
B) Destructor can throw, constructor cannot
C) Both can throw safely
D) Neither can throw
Answer: A) Constructor can throw, destructor should not
23. Which of the following is true for exception propagation?
A) If not caught in one function, exception propagates to caller
B) Exceptions disappear automatically
C) Only local functions can catch them
D) Exception must be caught in same block
Answer: A) If not caught in one function, exception propagates to caller
24. Which keyword combination is used to declare that a function might throw exceptions?
A) maythrow
B) throw()
C) exception()
D) catch()
Answer: B) throw()
25. What is the main purpose of exception handling in C++?
A) To reduce code length
B) To improve performance
C) To handle runtime errors gracefully
D) To remove bugs
Answer: C) To handle runtime errors gracefully
26. Which of the following is not a valid standard exception in C++?
A) bad_cast
B) bad_alloc
C) logic_error
D) type_error
Answer: D) type_error
27. Which standard exception is thrown when new fails to allocate memory?
A) out_of_range
B) runtime_error
C) bad_alloc
D) overflow_error
Answer: C) bad_alloc
28. What is the correct header file for standard exceptions in C++?
A) iostream
B) stdio.h
C)
D)
Answer: C)
29. Which standard exception is used when an invalid type conversion is detected?
A) bad_alloc
B) bad_cast
C) overflow_error
D) invalid_error
Answer: B) bad_cast
30. Which keyword in C++ is used to indicate that a block of code may cause an exception?
A) catch
B) throw
C) try
D) finally
Answer: C) try
31. Which of the following can handle all types of exceptions?
A) catch(int e)
B) catch(string s)
C) catch(exception e)
D) catch(…)
Answer: D) catch(…)
32. What is the output if no exception is thrown inside the try block?
A) Catch block executes
B) Program terminates
C) Program skips the catch block
D) Error occurs
Answer: C) Program skips the catch block
33. Can one try block have multiple catch blocks in C++?
A) No
B) Only two
C) Only one
D) Yes, multiple catch blocks are allowed
Answer: D) Yes, multiple catch blocks are allowed
34. What is the order of evaluation for multiple catch blocks?
A) Alphabetically
B) Random
C) Top to bottom
D) Bottom to top
Answer: C) Top to bottom
35. Which of the following errors cannot be handled using exceptions?
A) Memory allocation failure
B) Divide by zero
C) Syntax error
D) Out of range
Answer: C) Syntax error
36. Which of these describes exception handling correctly?
A) Error detection
B) Error detection and recovery
C) Error prevention
D) Error removal
Answer: B) Error detection and recovery
37. What happens when an exception is thrown inside a function and not caught there?
A) Ignored
B) Propagated to the calling function
C) Program ends
D) Stored in memory
Answer: B) Propagated to the calling function
38. Which is the correct syntax to throw an exception in C++?
A) throw;
B) throw exception_object;
C) catch exception_object;
D) raise exception;
Answer: B) throw exception_object;
39. When does stack unwinding occur in exception handling?
A) When stack overflows
B) During compilation
C) When exception is thrown and not caught in current scope
D) Never
Answer: C) When exception is thrown and not caught in current scope
40. Which of these is true about try blocks?
A) They must be followed by finally
B) They must be followed by at least one catch or a function-try-block
C) They can be standalone
D) They are optional
Answer: B) They must be followed by at least one catch or a function-try-block
41. What will the following code do?
try {
throw 5;
} catch (int e) {
cout << e;
}
A) Error
B) Prints 5
C) No output
D) Terminates
Answer: B) Prints 5
42. What will this code print?
try {
throw ‘A’;
} catch (int x) {
cout << “int”;
} catch (…) {
cout << “other”;
}
A) int
B) other
C) A
D) No output
Answer: B) other
43. Which of the following is not a valid handler?
A) catch(int)
B) catch(double)
C) catch(void)
D) catch(…)
Answer: C) catch(void)
44. Can a catch block rethrow an exception?
A) No
B) Yes, using throw; statement
C) Yes, using raise;
D) Only once
Answer: B) Yes, using throw; statement
45. Which standard exception indicates arithmetic overflow?
A) logic_error
B) runtime_error
C) overflow_error
D) underflow_error
Answer: C) overflow_error
46. Which type of error does out_of_range exception represent?
A) Accessing container element beyond valid index
B) Invalid cast
C) Divide by zero
D) Syntax error
Answer: A) Accessing container element beyond valid index
47. Which statement about try-catch blocks is true?
A) Only one try block per function is allowed
B) A try block can have multiple catch blocks
C) Catch block must be inside try
D) Try and catch cannot be nested
Answer: B) A try block can have multiple catch blocks
48. Which keyword restricts a function from throwing exceptions in C++11 and later?
A) throw()
B) maythrow
C) noexcept
D) noreturn
Answer: C) noexcept
49. What happens if an exception is thrown before object creation completes?
A) Object is created
B) Constructor is aborted and partially constructed objects are destroyed
C) Object remains half-created
D) Compiler ignores it
Answer: B) Constructor is aborted and partially constructed objects are destroyed
50. Which statement best describes try blocks in function declarations?
A) They can’t exist
B) They must appear after main()
C) They can wrap entire function bodies (function-try-blocks)
D) They are only for constructors
Answer: C) They can wrap entire function bodies (function-try-blocks)
51. Which of the following can be caught by a catch(…) handler?
A) Only integer exceptions
B) Only standard exceptions
C) All types of exceptions
D) Only user-defined exceptions
Answer: C) All types of exceptions
52. What is the role of the terminate() function in C++ exception handling?
A) Catches exception automatically
B) Called when no matching catch handler is found
C) Cleans memory
D) Prints error message
Answer: B) Called when no matching catch handler is found
53. Which function can be used to set a custom terminate handler?
A) seterrorhandler()
B) handleterminate()
C) set_terminate()
D) customterminate()
Answer: C) set_terminate()
54. Which standard exception class is thrown when invalid argument is passed to a function?
A) overflow_error
B) range_error
C) invalid_argument
D) domain_error
Answer: C) invalid_argument
55. Which standard header provides logic_error and runtime_error?
A)
B)
C)
D)
Answer: D)
56. Which of the following best defines “event handling”?
A) Compilation process
B) Responding to user or system-generated actions
C) Exception propagation
D) Error logging
Answer: B) Responding to user or system-generated actions
57. What is an “event” in programming?
A) Loop iteration
B) Variable declaration
C) Action that triggers a specific response in a program
D) Compiler instruction
Answer: C) Action that triggers a specific response in a program
58. What is the relationship between events and exceptions in C++?
A) Both are unrelated
B) Both are runtime errors
C) Events are expected triggers; exceptions are unexpected triggers
D) Events can’t occur at runtime
Answer: C) Events are expected triggers; exceptions are unexpected triggers
59. Which of the following can be considered an event?
A) Compiler error
B) Mouse click
C) Memory leak
D) Divide by zero
Answer: B) Mouse click
60. Can event-driven programming be implemented in C++?
A) No, only in Java
B) Yes, using callback functions and listeners
C) Only with exceptions
D) Only in C#
Answer: B) Yes, using callback functions and listeners
61. Which function is used to manually call the terminate handler?
A) terminate()
B) throw()
C) abort()
D) catch()
Answer: A) terminate()
62. What is the purpose of unexpected() function in older C++ versions?
A) Throws exception
B) Called when a function throws an exception not listed in its throw specification
C) Handles runtime errors
D) Declares exception types
Answer: B) Called when a function throws an exception not listed in its throw specification
63. Which of the following exceptions is derived from logic_error?
A) bad_alloc
B) overflow_error
C) invalid_argument
D) bad_cast
Answer: C) invalid_argument
64. Which exception class is used for domain errors in mathematical operations?
A) range_error
B) invalid_argument
C) domain_error
D) logic_error
Answer: C) domain_error
65. Which exception is thrown for an error in numerical result that is too large to represent?
A) underflow_error
B) overflow_error
C) domain_error
D) bad_alloc
Answer: B) overflow_error
66. Which of the following exception types is used for runtime errors not caused by logic errors?
A) logic_error
B) runtime_error
C) compile_error
D) syntax_error
Answer: B) runtime_error
67. Can exceptions be nested in C++?
A) No
B) Yes, a catch block can throw another exception
C) Only in classes
D) Only in constructors
Answer: B) Yes, a catch block can throw another exception
68. Which statement about custom exception classes is correct?
A) They must be inherited from logic_error
B) They can be derived from the standard exception class
C) They must use private inheritance
D) They cannot use constructors
Answer: B) They can be derived from the standard exception class
69. What will this code print?
try {
throw string(“Error”);
} catch (const char* msg) {
cout << “C-string”;
} catch (string s) {
cout << s;
}
A) C-string
B) Error
C) Runtime Error
D) No output
Answer: B) Error
70. Which exception type is thrown when type conversion fails in dynamic_cast?
A) runtime_error
B) bad_cast
C) bad_alloc
D) overflow_error
Answer: B) bad_cast
71. Which keyword combination in C++ specifies a function that guarantees no exception throwing?
A) maythrow()
B) throw()
C) noexcept(false)
D) noexcept(true)
Answer: D) noexcept(true)
72. Which of the following is true about event-driven systems?
A) They wait for input and respond accordingly
B) They execute sequentially
C) They ignore external actions
D) They don’t use functions
Answer: A) They wait for input and respond accordingly
73. Which of the following can represent an event source in C++ GUI systems?
A) Button widget
B) Loop
C) Integer
D) Class template
Answer: A) Button widget
74. Which statement best defines “callback function”?
A) A function that throws exceptions
B) A function that never returns
C) A function invoked when a specific event occurs
D) A function that handles errors
Answer: C) A function invoked when a specific event occurs
75. Which library is commonly used for event handling in C++ GUI applications?
A) stdio.h
B) Qt
C) STL
D) stdexcept
Answer: B) Qt
76. What is the return type of the what() function in the exception class?
A) string
B) char
C) const char*
D) void
Answer: C) const char*
77. What is the base class for all logic-related exceptions in C++?
A) runtime_error
B) domain_error
C) logic_error
D) exception
Answer: C) logic_error
78. Which of the following exceptions is derived from runtime_error?
A) invalid_argument
B) range_error
C) domain_error
D) length_error
Answer: B) range_error
79. What is the difference between runtime_error and logic_error?
A) runtime_error is checked at compile time
B) logic_error occurs at runtime
C) logic_error indicates programmer mistakes; runtime_error occurs during execution
D) No difference
Answer: C) logic_error indicates programmer mistakes; runtime_error occurs during execution
80. Which standard exception class is used for representing insufficient length of a container?
A) out_of_range
B) range_error
C) length_error
D) overflow_error
Answer: C) length_error
81. Which keyword combination is used to specify that a function may throw any exception?
A) throw(…)
B) throw(any)
C) maythrow()
D) catch(…)
Answer: A) throw(…)
82. Which function type is typically used to handle events in GUI programming?
A) Callback functions
B) Template functions
C) Static functions
D) Exception handlers
Answer: A) Callback functions
83. In which scenario should exceptions be preferred over return codes?
A) When error handling must not clutter normal code flow
B) When performance is more important than clarity
C) When code has no error conditions
D) When debugging
Answer: A) When error handling must not clutter normal code flow
84. What is the main difference between error codes and exceptions?
A) Error codes are faster
B) Exceptions separate error handling logic from normal logic
C) Error codes always terminate the program
D) Exceptions cannot propagate
Answer: B) Exceptions separate error handling logic from normal logic
85. Which operator can generate an exception related to memory allocation?
A) new[]
B) malloc
C) delete
D) new
Answer: D) new
86. What happens when new fails to allocate memory and exceptions are disabled?
A) It returns NULL
B) It throws bad_alloc
C) It terminates program
D) It logs error
Answer: A) It returns NULL
87. Can exceptions be thrown in destructors?
A) Yes, but it is discouraged
B) No
C) Only with noexcept(false)
D) Only in global destructors
Answer: A) Yes, but it is discouraged
88. Which of the following statements about noexcept is true?
A) It guarantees that a function will not throw exceptions
B) It throws exception automatically
C) It disables try blocks
D) It logs exceptions
Answer: A) It guarantees that a function will not throw exceptions
89. What happens if an exception is thrown in a noexcept function?
A) It is ignored
B) Program calls terminate()
C) It is rethrown automatically
D) It logs error
Answer: B) Program calls terminate()
90. What is a “function-try-block” used for in C++?
A) Declaring exceptions
B) Catching exceptions thrown during function or constructor initialization
C) Handling syntax errors
D) Optimizing functions
Answer: B) Catching exceptions thrown during function or constructor initialization
91. Which C++ construct supports both event-driven and exception-driven design?
A) Classes
B) Delegates and callbacks
C) Macros
D) Preprocessor
Answer: B) Delegates and callbacks
92. In an event-driven system, what is the purpose of the “listener”?
A) To throw exceptions
B) To wait for and respond to specific events
C) To log errors
D) To allocate memory
Answer: B) To wait for and respond to specific events
93. Which of the following is an example of an event source?
A) Variable assignment
B) Arithmetic operator
C) Keyboard press
D) Preprocessor directive
Answer: C) Keyboard press
94. What does the following code print?
try {
throw 10;
} catch (char e) {
cout << “char”;
} catch (int e) {
cout << “int”;
}
A) char
B) error
C) int
D) nothing
Answer: C) int
95. Which of the following exceptions is not part of the C++ standard exception hierarchy?
A) bad_cast
B) bad_typeid
C) bad_alloc
D) bad_pointer
Answer: D) bad_pointer
96. Can throw be used outside a try block?
A) Yes, always
B) Only inside a function
C) Yes, but it must be caught somewhere higher in the call stack
D) No, compiler error
Answer: C) Yes, but it must be caught somewhere higher in the call stack
97. What happens when catch parameter type mismatches the thrown type?
A) The compiler fixes it
B) Catch block is skipped and next handler is tried
C) Program stops
D) Type conversion occurs
Answer: B) Catch block is skipped and next handler is tried
98. Which type of event occurs without direct user action?
A) System event
B) Input event
C) Hardware interrupt
D) GUI event
Answer: A) System event
99. In C++ event systems, what is a “signal”?
A) A syntax rule
B) A C preprocessor directive
C) A notification that an event has occurred
D) An exception type
Answer: C) A notification that an event has occurred
100. What is the best practice for writing exception-safe code?
A) Use as many try blocks as possible
B) Avoid constructors
C) Ensure resources are released using RAII and smart pointers
D) Disable exceptions
Answer: C) Ensure resources are released using RAII and smart pointers
