Exception Handling Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q1
What is the purpose of exception handling in Python?
  • A To terminate the program immediately when an error occurs
  • B To handle errors gracefully and continue program execution
  • C To ignore all errors in the program
  • D To make the program run faster
Answer: Option B
Explanation: Exception handling allows programs to handle errors gracefully and continue execution.
Q2
Which keyword is used to handle exceptions in Python?
  • A catch
  • B throw
  • C try-except
  • D handle
Answer: Option C
Explanation: The try-except block is used to catch and handle exceptions in Python.
Q3
What is the base class for all exceptions in Python?
  • A Exception
  • B BaseException
  • C Error
  • D BaseError
Answer: Option B
Explanation: BaseException is the base class for all built-in exceptions in Python.
Q4
Which exception is raised when a variable is not found?
  • A VariableError
  • B NotFoundError
  • C NameError
  • D UndefinedError
Answer: Option C
Explanation: NameError is raised when a local or global name is not found.
Q5
What does the "finally" block do in exception handling?
  • A Executes only when no exception occurs
  • B Executes only when an exception occurs
  • C Always executes regardless of exceptions
  • D Never executes
Answer: Option C
Explanation: The finally block always executes, regardless of whether an exception occurred or not.
Q6
Which exception is raised when dividing by zero?
  • A DivisionError
  • B MathError
  • C ZeroDivisionError
  • D ArithmeticError
Answer: Option C
Explanation: ZeroDivisionError is raised when the second argument of division or modulo operation is zero.
Q7
What is the purpose of the "else" clause in try-except blocks?
  • A Executes when an exception occurs
  • B Executes only if no exception occurs in try block
  • C Always executes
  • D Executes before the try block
Answer: Option B
Explanation: The else clause executes only if no exception was raised in the try block.
Q8
How can you raise an exception manually in Python?
  • A throw
  • B raise
  • C exception
  • D trigger
Answer: Option B
Explanation: The raise statement is used to manually trigger exceptions in Python.
Q9
Which exception is raised when accessing a non-existent list index?
  • A KeyError
  • B IndexError
  • C ListError
  • D RangeError
Answer: Option B
Explanation: IndexError is raised when a sequence subscript is out of range.
Q10
What does the "except Exception as e" syntax do?
  • A Catches all exceptions and stores in e
  • B Catches only Exception class and stores in e
  • C Ignores all exceptions
  • D Raises a new exception
Answer: Option B
Explanation: This syntax catches all exceptions derived from Exception class and assigns the exception object to variable e.
Questions and Answers for Competitive Exams Various Entrance Test