Exception Handling Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q21
Which method is called when using the "with" statement for resource management?
  • A start() and stop()
  • B open() and close()
  • C __enter__() and __exit__()
  • D begin() and end()
Answer: Option C
Explanation: The with statement calls the __enter__ method at start and __exit__ method at end for context management.
Q22
What exception is raised when a file cannot be found?
  • A IOError
  • B FileError
  • C FileNotFoundError
  • D NotFoundError
Answer: Option C
Explanation: FileNotFoundError is raised when a file or directory is requested but doesn't exist.
Q23
How can you catch multiple exceptions in a single except block?
  • A Using multiple except blocks for each
  • B Using a tuple of exception types
  • C Using the "multiple" keyword
  • D It's not possible to catch multiple exceptions
Answer: Option B
Explanation: Multiple exceptions can be caught by specifying them as a tuple in the except clause.
Q24
What is the purpose of the traceback module?
  • A To prevent exceptions
  • B To handle exceptions automatically
  • C To extract and print stack traces
  • D To create new exceptions
Answer: Option C
Explanation: The traceback module provides utilities for extracting, formatting, and printing stack traces.
Q25
Which exception is raised when there is an indentation error?
  • A SyntaxError
  • B IndentationError
  • C FormatError
  • D TabError
Answer: Option B
Explanation: IndentationError is raised when there is incorrect indentation in the code.
Questions and Answers for Competitive Exams Various Entrance Test