Chapter-1 Exception Handling in Python — Online MCQ Test
COMPUTER SCIENCE · Grade 12 · CBSE(NCERT)
Practice Chapter-1 Exception Handling in Python with a free chapter-wise online MCQ test.
This chapter covers: try - except - else - finally - runtime errors - exception handling.
AI-generated questions from basic to board-exam level, with instant results and explanations.
Chapter-1 Exception Handling in Python — Important Questions & Answers
What is an exception in Python?
- A. A type of loop used in programming
- B. An event that occurs during program execution that disrupts the normal flow of the program
- C. A function that always returns an error
- D. A variable that stores error messages
Answer: B. An event that occurs during program execution that disrupts the normal flow of the program
An exception is an error event that disrupts normal program execution, which Python handles using exception handling mechanisms.
An exception is an error event that disrupts normal program execution, which Python handles using exception handling mechanisms.
Which keyword is used to catch an exception in Python?
- A. catch
- B. handle
- C. except
- D. error
Answer: C. except
The 'except' keyword is used in Python to catch and handle exceptions that occur in the try block.
The 'except' keyword is used in Python to catch and handle exceptions that occur in the try block.
What is the purpose of the 'try' block?
- A. To define a function
- B. To contain code that might raise an exception
- C. To repeat code multiple times
- D. To create a loop structure
Answer: B. To contain code that might raise an exception
The try block contains the code that might potentially raise an exception during execution.
The try block contains the code that might potentially raise an exception during execution.
Which of the following is a runtime error in Python?
- A. Syntax error
- B. IndentationError
- C. ZeroDivisionError
- D. NameError in import statement
Answer: C. ZeroDivisionError
ZeroDivisionError is a runtime error that occurs when attempting to divide a number by zero during program execution.
ZeroDivisionError is a runtime error that occurs when attempting to divide a number by zero during program execution.
What happens if an exception is not caught in a try-except block?
- A. The program continues normally
- B. The program terminates and displays an error message
- C. The program ignores the error
- D. The program automatically fixes the error
Answer: B. The program terminates and displays an error message
If an uncaught exception occurs, the program terminates and Python displays a traceback showing the error details.
If an uncaught exception occurs, the program terminates and Python displays a traceback showing the error details.
What is the difference between 'except Exception:' and 'except ValueError:'?
- A. They are exactly the same
- B. Exception catches all exceptions, while ValueError catches only value-related errors
- C. ValueError is more general than Exception
- D. Exception is outdated and should not be used
Answer: B. Exception catches all exceptions, while ValueError catches only value-related errors
Exception is a base class that catches all exceptions, while ValueError specifically catches exceptions related to invalid values.
Exception is a base class that catches all exceptions, while ValueError specifically catches exceptions related to invalid values.
Which exception would be caught by 'except (ValueError, TypeError):'?
- A. Only ValueError
- B. Only TypeError
- C. Either ValueError or TypeError
- D. None of the above
Answer: C. Either ValueError or TypeError
Using a tuple of exception types allows catching multiple specific exceptions in a single except block.
Using a tuple of exception types allows catching multiple specific exceptions in a single except block.
What happens when an exception occurs in the finally block?
- A. It is ignored
- B. It propagates up and can replace the original exception
- C. It is automatically caught
- D. The program restarts
Answer: B. It propagates up and can replace the original exception
If an exception occurs in the finally block, it propagates up and can replace or mask the original exception from the try block.
If an exception occurs in the finally block, it propagates up and can replace or mask the original exception from the try block.
What is the output of this code? try: raise ValueError('Invalid value') except ValueError as e: print(f'Caught: {e}') finally: print('Cleanup')
- A. Caught: Invalid value\nCleanup
- B. Cleanup
- C. Caught: Invalid value
- D. Invalid value
Answer: A. Caught: Invalid value\nCleanup
The ValueError is raised and caught, printing 'Caught: Invalid value', then finally block executes printing 'Cleanup' on a new line.
The ValueError is raised and caught, printing 'Caught: Invalid value', then finally block executes printing 'Cleanup' on a new line.
Which code will NOT raise an exception when executed?
- A. x = int('123abc')
- B. y = {'a': 1}['b']
- C. z = (1, 2, 3)[1]
- D. w = 5 / 0
Answer: C. z = (1, 2, 3)[1]
Accessing index 1 of tuple (1,2,3) returns 2 and executes without error, while other options raise ValueError, KeyError, and ZeroDivisionError respectively.
Accessing index 1 of tuple (1,2,3) returns 2 and executes without error, while other options raise ValueError, KeyError, and ZeroDivisionError respectively.