0% found this document useful (0 votes)
481 views4 pages

Exception Handling - Worksheet 1 - 1 Marks

This document is a worksheet on exception handling in Python, containing multiple-choice questions and code snippets related to try-except blocks, finally blocks, and various exceptions. It tests knowledge on the purpose of exception handling, standard built-in exceptions, and the behavior of code when exceptions occur. The worksheet includes questions about assertions, outputs of code snippets, and the handling of user inputs.

Uploaded by

riyanishu2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
481 views4 pages

Exception Handling - Worksheet 1 - 1 Marks

This document is a worksheet on exception handling in Python, containing multiple-choice questions and code snippets related to try-except blocks, finally blocks, and various exceptions. It tests knowledge on the purpose of exception handling, standard built-in exceptions, and the behavior of code when exceptions occur. The worksheet includes questions about assertions, outputs of code snippets, and the handling of user inputs.

Uploaded by

riyanishu2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exception Handling

Worksheet – 1
1. In a try-except block, can there be multiple 'except' clauses?
a) No, there can be only one 'except' clause.
b) Yes, but only if the exceptions are of the same type.
c) Yes, it allows handling different exceptions separately.
d) No, 'except' clauses are not allowed in a try-except block.

2. When might you use the 'finally' block in exception handling?

a) To handle exceptions that are expected to occur frequently.


b) To provide a resolution for every possible error.
c) To close resources that were opened in the 'try' block, regardless of whether an
exception occurred or not.
d) To avoid having to use 'except' blocks.

3. What will be the output of the following code snippet?

a) Division by zero! b) Arithmetic error occurred!


c) No error! d) This code will raise a syntax error.
4. Which of the following is NOT a standard built-in exception in Python?
a) ValueError b) IndexError c) NullPointerException d) KeyError
5. What is an exception in programming?
a) An error that occurs during runtime b) A warning message from the compiler
c) A comment in the cod d) A statement that terminates the program
6. What is the purpose of the "try" block in a try-except construct?
a) To handle the exception by executing specific code
b) To specify the type of exception to be thrown
c) To define a custom exception class
d) To ensure a specific block of code always executes

7. Which of the following exceptions in Python is not a built-in exception?


a) ValueError b) KeyError c) CustomError d) IndexError

A. Both A and R are true and R is correct explanation of A


B. Both A and R are true but R is not correct explanation of A
C. A is True but R is False
D. R is True but A is False

Exception Handling – Worksheet 1 – 1 Marks Page 1


8. Assertion (A): In Python, the "try" block is used to enclose code that might raise an
exception.
Reasoning (R): The "try" block is where the program attempts to execute code that might
result in an exception. If an exception occurs, it is handled in the corresponding "except"
block.
9. Assertion (A): The "finally" block in Python is always executed, regardless of whether
an exception is raised or not.
Reasoning (R): The "finally" block contains code that is guaranteed to execute, whether
an exception occurs within the "try" block or not.
10. Assertion (A): Python allows multiple "except" blocks to be used within a single "try"
block to handle different exceptions.
Reasoning (R): By using multiple "except" blocks with different exception types, Python
provides the flexibility to handle various types of exceptions separately.
11. Code snippet:

Predict the output when:


a) The user enters "0" . b) The user enters "5".
c) The user enters "abc".
12. State whether the following statement is True or False:
An exception may be raised even if the program is syntactically correct.
13. What will be the output of the following code if the input is “e”:
try:
value = int("abc")
result = 10 / 0
except ValueError:
print("Error: Invalid value conversion")
except ZeroDivisionError:
print("Error: Division by zero")
14. What will be the output of the following code if the input is: i. 2 ii. 2.2
try:
num = int(input("Enter a number: "))
except ValueError:
print("Error: Invalid input")
else:
print("Entered number: ",num)

15.Rewrite the following code after handling all possible exceptions.


num = int(input("Enter a number: "))

Exception Handling – Worksheet 1 – 1 Marks Page 2


result = 10 / num
print("Result:", result)

16. Consider the code given below:


L = [‘s’, 45, 23]
Result = 0
for x in L:
print (“The element is “, x)
Result += x
print(“The addition of all elements of L is: “, Result)
Which of the following error will be raised by the given Python code?
a) NameError b) ValueError c) TypeError d) IOError
17. Code snippet:

Predict the output when:


a) The user enters "10" for both numbers.
b) The user enters "5" for the first number and "0" for the second number.
c) The user enters "abc" for both numbers.

18. Which of the following statements is true?


a). The standard exceptions are automatically imported in Python programs.
b). All raised standard exceptions must be handled in Python.
c). When there is deviation from the rules of a programming language, a semantic error
is thrown.
d). If any exception is thrown in try block, else block is executed.
19. Identify the statement(s) from the following options which will raise TypeError
exception(s):
a) print('5') b) print( 5 * 3) c) print('5' +3) d) print('5' + '3')
20. What is an exception in Python?
a) An error b) A warning c) A function d) A class

21. Which keyword is used to raise an exception explicitly in Python?


a) catch b) throw c) raise d) try

22. What is the purpose of the 'finally' block in a try-except statement?


a) To handle exceptions b) To specify alternative code

Exception Handling – Worksheet 1 – 1 Marks Page 3


c) To execute cleanup code d) To suppress errors

23. Which of the following is not an exception handling clause in Python?


a) try b) except c) else d) throw

24. What will be the output of the following code?


try:
print(10 / 0)
except ZeroDivisionError:
print("Error: Division by zero")
a) Error: Division by zero b) Error: Index out of range

c) 10 d) None of the above

25. In a try-except block, if an exception occurs but it is not handled by any except
clause, what happens?
a) The program terminates b) The exception is ignored
c) The program continues to execute normally d) None of the above

Exception Handling – Worksheet 1 – 1 Marks Page 4

You might also like