0% found this document useful (0 votes)
17 views

exception-assignments-2

The document contains a series of questions and programming tasks related to exception handling in Python. It covers topics such as the use of try-except blocks, the purpose of finally blocks, built-in exceptions, and assertions about exception handling behavior. Additionally, it includes programming exercises that require implementing exception handling in various scenarios.

Uploaded by

agarwalpratik43
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

exception-assignments-2

The document contains a series of questions and programming tasks related to exception handling in Python. It covers topics such as the use of try-except blocks, the purpose of finally blocks, built-in exceptions, and assertions about exception handling behavior. Additionally, it includes programming exercises that require implementing exception handling in various scenarios.

Uploaded by

agarwalpratik43
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise

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 code
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

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

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.
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:

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


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: "))
result = 10 / num
print("Result:", result)

16 Consider the code given below:

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:

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')

Programming based question:


1 Create a simple calculator program that takes two numbers and an operator (+, -,
*, /) as input. Implement exception handling to handle cases like division by zero
and invalid operators.

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.

3 Create a program that connects to a database and performs database operations


(e.g., insert, update, delete). Use exception handling to deal with database-related
exceptions, such as connection errors or SQL syntax error.

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.

You might also like