Exception Handling Questions and Answers
Practice ModeShowing 10 of 25 questions
Q11
Which exception is raised when a dictionary key is not found?
Answer: Option A
Explanation: KeyError is raised when a dictionary key is not found in the set of existing keys.
Q12
What is the difference between SyntaxError and Exception?
Answer: Option B
Explanation: SyntaxError occurs during parsing due to invalid syntax, while Exception occurs during execution.
Q13
How can you create a custom exception in Python?
Answer: Option B
Explanation: Custom exceptions are created by defining a new class that inherits from Exception or its subclasses.
Q14
What happens if an exception is not caught in Python?
Answer: Option C
Explanation: Uncaught exceptions propagate up the call stack and terminate the program with a traceback.
Q15
Which exception is raised when converting an invalid string to integer?
Answer: Option C
Explanation: ValueError is raised when a function receives an argument of correct type but inappropriate value.
Q16
What is the purpose of multiple except blocks?
Answer: Option B
Explanation: Multiple except blocks allow handling different exception types with specific handlers.
Q17
Which exception is raised when importing a module that cannot be found?
Answer: Option B
Explanation: ImportError is raised when the import statement has trouble loading a module.
Q18
What does the "assert" statement do in Python?
Answer: Option B
Explanation: The assert statement is used for debugging and raises AssertionError if the condition is False.
Q19
Which exception is raised when performing an operation on incompatible types?
Answer: Option B
Explanation: TypeError is raised when an operation or function is applied to an object of inappropriate type.
Q20
What is the difference between except: and except Exception:
Answer: Option B
Explanation: except: catches all exceptions including system-exiting ones, while except Exception: catches only regular exceptions.