exception-assignments-2
exception-assignments-2
a) Division by zero!
b) Arithmetic error occurred!
c) No error!
d) This code will raise a syntax error.
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.
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
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.
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
11 Code snippet:
12 State whether the following statement is True or False: An exception may be raised
even if the program is syntactically correct.
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)
Result = 0
for x in L:
Result += x
Which of the following error will be raised by the given Python code?
a) NameError
b) ValueError
c) TypeError
d) IOError
17 Code snippet:
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')
2 Build a program that asks the user for an integer input. Use exception handling to
ensure that the input is a valid integer. If the user provides invalid input, prompt
them to retry until a valid integer is entered.
4 Create a program that reads data from a file specified by the user. Implement
exception handling to catch and handle the "FileNotFoundError" exception if the
file does not exist.
5 Define a dictionary with some key-value pairs. Ask the user for a key and attempt
to access the corresponding value from the dictionary. Handle the "KeyError"
exception and display a message if the key is not found.