Exception Handling Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
Which exception is raised when a dictionary key is not found?
  • A KeyError
  • B DictError
  • C NotFoundError
  • D IndexError
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?
  • A SyntaxError occurs at runtime, Exception at compile time
  • B SyntaxError occurs during parsing, Exception during execution
  • C Both are the same
  • D Exception occurs during parsing, SyntaxError during execution
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?
  • A Using the "custom" keyword
  • B By creating a class that inherits from Exception
  • C Using the "new_exception" function
  • D Custom exceptions are not possible 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?
  • A The program continues silently
  • B The exception is logged and ignored
  • C The program terminates with a traceback
  • D The exception is automatically fixed
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?
  • A TypeError
  • B ConversionError
  • C ValueError
  • D InvalidError
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?
  • A To handle the same exception multiple times
  • B To handle different exception types specifically
  • C To make code run faster
  • D To ignore multiple exceptions
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?
  • A ModuleError
  • B ImportError
  • C NotFoundError
  • D LoadError
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?
  • A Always raises an exception
  • B Tests a condition and raises AssertionError if False
  • C Ignores all exceptions
  • D Creates a new variable
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?
  • A OperationError
  • B TypeError
  • C IncompatibleError
  • D ValueError
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:
  • A There is no difference
  • B except: catches all, except Exception: catches only regular exceptions
  • C except Exception: catches more exceptions
  • D except: is faster
Answer: Option B
Explanation: except: catches all exceptions including system-exiting ones, while except Exception: catches only regular exceptions.
Questions and Answers for Competitive Exams Various Entrance Test