1. Which of the following is raised when the denominator in a division operation is zero?
(a) ValueError
(b) TypeError
(c) ZeroDivisionError
(d) NameError
2. The assert statement is used to:
(a) Catch exceptions
(b) Test an expression and raise AssertionError if false
(c) Close a file
(d) Define a user-defined exception
3. Which clause is always executed regardless of whether an exception occurs or not?
(a) try
(b) except
(c) else
(d) finally
4. SyntaxError is raised when:
(a) A file cannot be opened
(b) There is an error in the syntax of the Python code
(c) The end of file is reached
(d) A variable is not defined
5. The process of creating an exception object and handing it over to the runtime system is called:
(a) Catching an exception
(b) Throwing an exception
(c) Handling an exception
(d) Raising an exception
6. Which of the following is used to forcefully raise an exception?
(a) throw
(b) raise
(c) catch
(d) assert (only)
7. Assertion (A): The statements inside the finally block are always executed.
Reason (R): The finally block is used to ensure that important cleanup code runs irrespective of whether an exception occurred.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and 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. IndexError is raised when:
(a) A variable name is not defined
(b) The index or subscript in a sequence is out of range
(c) Incorrect indentation is used
(d) A module is not found
9. Assertion (A): Every SyntaxError is an exception but every exception cannot be a syntax error.
Reason (R): Exceptions can occur even when the program is syntactically correct.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and 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.
10. In a try...except block, if no exception is raised, which block (if present) gets executed?
(a) except
(b) finally
(c) else
(d) Both except and finally
11. Define the term ‘exception’ as per the chapter.
12. Differentiate between Syntax Error and Exception.
13. What is the use of the raise statement? Give its syntax.
14. What will be the output of the following code?
python
print("use of assert statement")
def negativecheck(number):
assert(number >= 0), "OOPS... Negative Number"
print(number * number)
print(negativecheck(100))
print(negativecheck(-350))
15. Name any four built-in exceptions mentioned in the chapter along with the situation in which they are raised.
16. What is the difference between raise and assert statements?
17. Explain the need for exception handling in Python programs.
18. Consider the following code and answer the questions that follow:
python
numerator = 50
denom = int(input("Enter the denominator: "))
quotient = (numerator / denom)
print(quotient)
Identify the exceptions that may occur in the above code and write the code with appropriate except blocks to handle them.
19. What is the role of the else clause in a try...except...else block? Explain with the help of an example from the chapter.
20. Rewrite the following code after identifying and correcting the error:
python
try:
num = int(input("Enter a number"))
result = 100 / num
except ZeroDivisionError
print("Cannot divide by zero")
print(result)
21. Explain the complete process of exception handling in Python as described in the chapter (throwing and catching an exception).
22. Write a complete Python program that accepts two numbers from the user and displays their quotient. The program should handle ZeroDivisionError and ValueError using appropriate exception handling. Use try...except...else...finally blocks.
23. Consider the following code:
python
print("Practicing for try block")
try:
numerator = 50
denom = int(input("Enter the denominator"))
quotient = (numerator / denom)
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO is not allowed")
else:
print("The result of division operation is", quotient)
finally:
print("OVER AND OUT")
Perform a dry run of the above code when the user enters the value 0 for the denominator. Show the execution using a trace table with columns: Statement, Value of denom, Exception Raised, Output.
24. Explain the working of multiple except blocks and the use of a general except clause (without specifying any exception) with suitable examples from the chapter.
25. A student is writing a program to read two numbers and perform division. The program should also handle situations where the user enters non-numeric data or zero as the denominator.
a) Which exception will be raised if the user enters a non-numeric value? (1 mark)
b) Write the try block for the division operation. (1 mark)
c) Write the except blocks to handle ZeroDivisionError and ValueError. (1 mark)
d) Where should the message “Operation completed” be placed so that it always gets displayed? Justify. (1 mark)
26. A programmer wants to ensure that a file is always closed even if an exception occurs while reading data from it.
a) Which clause should be used to ensure the file is closed? (1 mark)
b) Write the structure of try...except...finally block for file handling. (1 mark)
c) What happens if an exception occurs for which no except block is defined? (1 mark)
d) Explain the role of the finally clause in such a scenario. (1 mark)
1. (c) ZeroDivisionError
2. (b) Test an expression and raise AssertionError if false
3. (d) finally
4. (b) There is an error in the syntax of the Python code
5. (b) Throwing an exception
6. (b) raise
7. (a) Both A and R are true and R is the correct explanation of A.
8. (b) The index or subscript in a sequence is out of range
9. (a) Both A and R are true and R is the correct explanation of A.
10. (c) else
11. An exception is a Python object that represents an error. When an error occurs during the execution of a program, an exception is said to have been raised.
12. Syntax errors are detected when rules of Python are not followed (parsing errors). Exceptions occur during execution even if the code is syntactically correct.
13. The raise statement is used to forcefully raise an exception. Syntax: raise exception-name[(optional argument)]
14. Output:
use of assert statement
10000
OOPS... Negative Number
(The second call raises AssertionError because the number is negative.)
15. ZeroDivisionError (denominator is zero), IndexError (index out of range), NameError (variable not defined), ValueError (inappropriate value for data type).
16. raise is used to throw a specific exception (built-in or user-defined). assert tests an expression and raises AssertionError if the expression is false.
17. Exception handling prevents the program from terminating abruptly. It separates the main logic from error detection and correction code. Python allows creation of specific handlers for different types of exceptions.
18. Possible exceptions: ZeroDivisionError, ValueError. Corrected code uses try...except ZeroDivisionError and try...except ValueError.
19. The else block is executed only when no exception is raised in the try block. It contains code that should run if the try block completes successfully.
20. Missing colon after except ZeroDivisionError. The print(result) should be inside the else block or after proper handling.
21. When an error occurs, Python creates an exception object and throws it. The runtime system searches the call stack for a matching handler (catching the exception). If found, the handler is executed.
22.
python
try:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
quotient = num1 / num2
except ZeroDivisionError:
print("Denominator as ZERO is not allowed")
except ValueError:
print("Only INTEGERS should be entered")
else:
print("The result of division operation is", quotient)
finally:
print("OVER AND OUT")
23. Trace table (when denom = 0):
| Statement | denom | Exception | Output |
|-----------|-------|-----------|--------|
| denom = int(...) | 0 | - | - |
| quotient = ... | - | ZeroDivisionError | - |
| except block | - | - | "Denominator as ZERO is not allowed" |
| finally block | - | - | "OVER AND OUT" |
24. Multiple except blocks allow handling different types of exceptions separately. A general except clause (without exception name) acts as a catch-all for any unhandled exception and should be placed last.
25. a) ValueError
b) try: numerator=50; denom=int(input(...)); quotient=(numerator/denom)
c) except ZeroDivisionError: and except ValueError: blocks
d) finally block — it always executes.
26. a) finally clause
b) try: ... except ...: ... finally: file.close()
c) The exception is re-raised after the finally block executes.
d) The finally clause ensures cleanup (like closing files) occurs even if an unhandled exception is raised.
All questions are answerable from the NCERT chapter text. Reviewed by GFIS faculty.