Exception handling in python, Study Guides, Projects, Research of Computer science

Exception handling in python with detailed explanation with examples

Typology: Study Guides, Projects, Research

2025/2026

Available from 01/24/2026

abhishek-voi
abhishek-voi 🇮🇳

5 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 1 EXCEPTION HANDLING IN PYTHON
MULTIPLE
CHOICE
QUESTIONS
(MCQs)
1.
What type of errors are exceptions in Python?
a) Logical errors b) Syntax errors c) Runtime errors d) Parsing errors
2.
What happens when a syntax error occurs in Python?
a) Program continues with incorrect output b) Program halts with a description of the error
c) Program fixes the error automatically d) Program warns but does not stop
3.
What are syntax errors also known as?
a) Runtime errors b) Logical errors c) Parsing errors d) None of the above
4.
Which Python mode provides immediate feedback on syntax errors?
a) Script mode b) Shell mode c) IDE mode d) Debug mode
5.
When does a ZeroDivisionError occur?
a) Dividing by a negative number b) Using a zero numerator
c) Dividing by zero d) Using zero in mathematical expressions
6.
What does Python do when an exception is raised during execution?
a) Continues execution b) Skips the error c) Terminates the program abruptly
d) Jumps to exception handling code if present
7.
Which exception is raised when a variable is not defined?
a) NameError b) ValueError c) SyntaxError d) TypeError
8.
What does the IOError exception indicate?
a) An undefined variable b) A file that cannot be opened
c) A division by zero d) An incorrect argument type
9.
Which exception is raised for incorrect indentation?
a) ValueError b) TypeError c) IndentationError d) SyntaxError
10.
Which keyword is used to manually raise an exception?
a) assert b) raise c) throw d) except
11.
What happens after an exception is raised using the raise statement?
a) The remaining statements in the block are executed b) The current block stops execution
c) Execution continues in the same block d) None of the above
12.
What exception does the assert statement raise if the condition is False?
a) AssertionError b) ValueError c) SyntaxError d) RuntimeError
13.
What is the main purpose of exception handling?
a) To debug syntax errors b) To prevent program crashes
c) To increase execution speed d) To optimize performance
14.
Which block is used to catch exceptions in Python?
a) try b) except c) else d) finally
15.
Which block is always executed, irrespective of whether an exception occurred?
a) try b) except c) else d) finally
16.
What happens if multiple except blocks are present for a single try block?
a) All are executed b) The first matching block is executed
c) None are executed d) Only the last block is executed
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Exception handling in python and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

CHAPTER 1 EXCEPTION HANDLING IN PYTHON

MULTIPLE CHOICE QUESTIONS (MCQs)

  1. What type of errors are exceptions in Python? a) Logical errors b) Syntax errors c) Runtime errors d) Parsing errors
  2. What happens when a syntax error occurs in Python? a) Program continues with incorrect output b) Program halts with a description of the error c) Program fixes the error automatically d) Program warns but does not stop
  3. What are syntax errors also known as? a) Runtime errors b) Logical errors c) Parsing errors d) None of the above
  4. Which Python mode provides immediate feedback on syntax errors? a) Script mode b) Shell mode c) IDE mode d) Debug mode
  5. When does a ZeroDivisionError occur? a) Dividing by a negative number b) Using a zero numerator c) Dividing by zero d) Using zero in mathematical expressions
  6. What does Python do when an exception is raised during execution? a) Continues execution b) Skips the error c) Terminates the program abruptly d) Jumps to exception handling code if present
  7. Which exception is raised when a variable is not defined? a) NameError b) ValueError c) SyntaxError d) TypeError
  8. What does the IOError exception indicate? a) An undefined variable b) A file that cannot be opened c) A division by zero d) An incorrect argument type
  9. Which exception is raised for incorrect indentation? a) ValueError b) TypeError c) IndentationError d) SyntaxError
  10. Which keyword is used to manually raise an exception? a) assert b) raise c) throw d) except
  11. What happens after an exception is raised using the raise statement? a) The remaining statements in the block are executed b) The current block stops execution c) Execution continues in the same block d) None of the above
  12. What exception does the assert statement raise if the condition is False? a) AssertionError b) ValueError c) SyntaxError d) RuntimeError
  13. What is the main purpose of exception handling? a) To debug syntax errors b) To prevent program crashes c) To increase execution speed d) To optimize performance
  14. Which block is used to catch exceptions in Python? a) try b) except c) else d) finally
  15. Which block is always executed, irrespective of whether an exception occurred? a) try b) except c) else d) finally
  16. What happens if multiple except blocks are present for a single try block? a) All are executed b) The first matching block is executed c) None are executed d) Only the last block is executed
  1. When is the else clause executed in a try…except block? a) If no exception occurs b) If an exception occurs c) Always executed d) Never executed
  2. Which block comes immediately after the try block? a) except b) else c) finally d) None
  3. What is the primary use of the finally block? a) To catch specific exceptions b) To execute cleanup code c) To handle syntax errors d) To ensure program optimization
  4. Which statement is true for the finally block? a) It executes only if an exception occurs b) It executes only if no exception occurs c) It executes regardless of exceptions d) It does not execute under any condition
  5. Which exception is handled in the following code? try : num = int(input("Enter a number: ")) except ValueError: print("Invalid input!") a) ZeroDivisionError b) SyntaxError c) ValueError d) TypeError
  6. What happens if no exception is raised in the try block? a) The except block executes b) The else block executes c) Both except and else blocks execute d) The program terminates
  7. What will happen if an exception not matched by any except block occurs? a) The program continues b) The program terminates c) The last except block is executed d) None of the above
  8. In a try…except block with multiple except clauses, which block is executed first? a) The first except block b) The most specific matching block c) The last except block d) None of the above
  9. What can be included in the raise statement for additional information? a) Exception name only b) Optional arguments like a string message c) Exception handling code d) None of the above
  10. What does the following code output? try : raise ValueError("Custom error message") except ValueError as e: print(e) a) Nothing b) “ValueError” c) “Custom error message” d) Program terminates with error
  11. Which exception is user-defined? a) ImportError b) ZeroDivisionError c) AssertionError d) Any exception created by the programmer
  12. What is printed by the following code? try : raise NameError("Example") except NameError: print("NameError occurred") a) NameError b) NameError occurred c) Example d) None of the above
  1. What is the purpose of an else clause in a try block? a) It is mandatory b) Executes if no exception occurs c) Always executes regardless of exceptions d) It defines error recovery steps
  2. Which block can have more than one occurrence in exception handling? a) try b) except c) else d) finally
  3. What is the output of the following code if the input is 0? try : result = 50 / int(input("Enter a number: ")) except ZeroDivisionError: print("Cannot divide by zero!") except ValueError: print("Invalid input!") finally : print("Execution complete.") a) Cannot divide by zero! Execution complete. b) Invalid input! Execution complete. c) 50 divided by 0 d) Program terminates with ZeroDivisionError
  4. Which exception is raised if the following code is executed and the input is abc? try : num = int(input("Enter a number: ")) except ValueError: print("Invalid input!") a) ValueError b) TypeError c) SyntaxError d) NameError
  5. What does exception handling ensure? a) Errors are ignored b) Code runs faster c) Program does not crash abruptly d) Syntax errors are corrected automatically
  6. What is the primary benefit of a try…finally structure? a) Better syntax b) Clean termination or recovery c) Handling specific exceptions d) Ignoring runtime errors
  7. hat exception is raised if input() encounters EOF? a) ValueError b) EOFError c) TypeError d) SyntaxError
  8. When is the OverFlowError raised? a) A file cannot be opened b) Division by zero occurs c) A calculation exceeds the max limit for a numeric type d) An undefined variable is accessed
  9. What is the process of finding an exception handler called? a) Raising an exception b) Catching an exception c) Searching the call stack d) None of the above
  10. What happens when an exception is successfully caught? a) It propagates b) Execution terminates c) Control resumes after the try block d) None of the above
  1. Assertion (A): Syntax errors are detected during the execution of a Python program. Reason (R): Syntax errors occur when the rules of the programming language are violated. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  2. Assertion (A): Exceptions disrupt the normal flow of program execution. Reason (R): Exceptions represent errors that occur during runtime and must be handled. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  3. Assertion (A): Built-in exceptions in Python handle commonly occurring errors. Reason (R): Programmers need to create user-defined exceptions for all error cases. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  4. Assertion (A): A ZeroDivisionError is raised when a division operation involves a denomina- tor of zero. Reason (R): Python checks for runtime errors during program execution. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  5. Assertion (A): The raise statement in Python can be used to manually raise exceptions. Reason (R): The raise statement forces an exception to be raised and handled. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  6. Assertion (A): The assert statement raises an exception if the given condition is false. Reason (R): The assert statement is used for debugging and testing in Python. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  7. Assertion (A): The try block must be followed by at least one except block. Reason (R): The try block allows execution of code that may raise exceptions, while the except block handles them. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  8. Assertion (A): The finally block is executed only when an exception is raised in the try block. Reason (R): The finally block ensures cleanup code is executed regardless of whether an exception occurred or not. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  1. Assertion (A): An EOFError is raised when the input() function reaches the end of a file without receiving any input. Reason (R): The EOFError occurs when the Python program encounters an empty input stream unexpectedly. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  2. Assertion (A): The finally block is executed even if an unhandled exception occurs in the try block. Reason (R): The finally block is designed to perform cleanup operations regardless of the program’s state. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  3. Assertion (A): The Python interpreter automatically searches the call stack to find an appropriate exception handler when an exception is raised. Reason (R): If an exception handler is not found in the call stack, the program terminates with a traceback. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  4. Assertion (A): The TypeError exception is raised when an operation is applied to an object of an inappropriate type. Reason (R): Python performs type-checking at runtime for all operations. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  5. Assertion (A): A generic except block can be used to catch any exception that is not explicitly named in previous except blocks. Reason (R): A generic except block should always be placed after all specific except blocks to avoid masking exceptions. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  6. Assertion (A): The try...except...else structure ensures that the else block is executed only if the try block executes without raising an exception. Reason (R): The else block in exception handling is useful for executing code that must run only when no exceptions occur. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  1. Assertion (A): The NameError exception is raised when a variable is referenced before it is defined. Reason (R): Undefined variables in Python automatically default to None. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  2. Assertion (A): Exception handling in Python can be applied to both built-in and user-defined exceptions. Reason (R): Python provides the raise statement to throw exceptions and the try...except block to handle them. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  3. Assertion (A): The IndentationError exception is raised when code is not properly indented in Python. Reason (R): Python enforces indentation as part of its syntax for defining blocks of code. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.
  4. Assertion (A): Exception handling improves program robustness and user experience. Reason (R): Exception handling prevents programs from crashing abruptly during runtime errors. a) Both Assertion (A) and Reason (R) are true, and (R) is the correct explanation of (A). b) Both Assertion (A) and Reason (R) are true, but (R) is not the correct explanation of (A). c) (A) is true, but (R) is false. d) (A) is false, but (R) is true.

FILL IN THE BLANKS

  1. A error occurs when the rules of the programming language are not followed.
  2. Errors that occur during program execution and disrupt its normal flow are called
  3. The block in Python is used to catch exceptions and handle them appropriately.
  4. A division by zero operation raises the exception.
  5. The exception is raised when a variable is referenced but has not been defined.
  6. The Python statement used to manually raise an exception is.
  7. A block is always executed, regardless of whether an exception occurred or not.
  8. The exception occurs when the file specified in a program cannot be opened.
  9. The process of identifying and handling exceptions is called.