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

Exception Handing - Worksheet

The document discusses exception handling in Python. It covers try-except blocks, built-in exceptions, raising exceptions, and using exceptions to handle errors. Multiple choice questions are provided about exception handling concepts in Python.
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)
175 views

Exception Handing - Worksheet

The document discusses exception handling in Python. It covers try-except blocks, built-in exceptions, raising exceptions, and using exceptions to handle errors. Multiple choice questions are provided about exception handling concepts in Python.
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/ 3

try:

value = int("abc")
result = 10 / 0
Exception Handing - Worksheet except ValueError:
print("Error: Invalid value conversion")
1. In a try-except block, can there be multiple 'except' clauses? except ZeroDivisionError:
a) No, there can be only one 'except' clause. print("Error: Division by zero")
b) Yes, but only if the exceptions are of the same type. 8. What will be the output of the following code if the input is:
c) Yes, it allows handling different exceptions separately. i. 2 ii. 2.2
d) No, 'except' clauses are not allowed in a try-except block. try:
2. When might you use the 'finally' block in exception handling? num = int(input("Enter a number: "))
a) To handle exceptions that are expected to occur frequently. except ValueError:
b) To provide a resolution for every possible error. print("Error: Invalid input")
c) To close resources those were opened in the 'try' block, regardless else:
of whether an exception occurred or not. print("Entered number: ",num)
d) To avoid having to use 'except' blocks. 9. Consider the code given below:
3. Which of the following is NOT a standard built-in exception in Python? L = [„s‟, 45, 23]
a) ValueError b) IndexError Result = 0
c) NullPointerException d) KeyError for x in L:
4. What is an exception in programming? print (“The element is “, x)
a) An error that occurs during runtime Result += x
b) A warning message from the compiler print(“The addition of all elements of L is: “, Result)
c) A comment in the code d) A statement that terminates the program Which of the following error will be raised by the given Python code?
5. Code snippet: a) NameError b) ValueError c) TypeError d) IOError
10. 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')
11. Which of the following statements is true?
a) The standard exceptions are automatically imported in Python
programs.
Predict the output when: b) All raised standard exceptions must be handled in Python.
a) The user enters "0" b) The user enters "5" c) When there is deviation from the rules of a programming language,
c) The user enters "abc" a semantic error is thrown.
6. State whether the following statement is True or False: An exception may d) If any exception is thrown in try block, else block is executed.
be raised even if the program is syntactically correct. Q12 and 13 are ASSERTION AND REASONING based questions.
7. What will be the output of the following code if the input is “e”: Mark the correct choice as,
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 try:
C. A is True but R is False D. R is True but A is False f_n = input("Enter file name")
12. Assertion (A): The "finally" block in Python is always executed, i_f = open(f_n, 'r')
regardless of whether an exception is raised or not. except:
Reasoning (R): The "finally" block contains code that is guaranteed to print("Input file not found")
execute, whether an exception occurs within the "try" a) No error b) Assertion error c) Input output error d) Name error
block or not. 20. What will be the output of the following Python code?
13. Assertion (A): Python allows multiple "except" blocks to be used within lst = [1, 2, 3]
a single "try" block to handle different exceptions. lst[3]
Reasoning (R): By using multiple "except" blocks with different a) NameError b) ValueError c) IndexError d) TypeError
exception types, Python provides the flexibility to 21. What will be the output of the following Python code?
handle various types of exceptions separately. >>> t1=[5,8,13]
14. >>> t[5]
a) IndexError b) NameError c) TypeError d) ValeError
22. What will be the output of the following Python code, if the time
module has already been imported?
15. When will the else part of try-except-else be executed? >>> 4 + '3'
a) always b) when an exception occurs a) NameError b) IndexError c) ValueError d) TypeError
c) when no exception occurs 23. What will be the output of the following Python code, if the time
d) when an exception occurs in to except block module has already been imported?
16. Can one block of except statements handle multiple exceptions? >>> 4 + '3'
a) yes, like except TypeError, SyntaxError ,… a) NameError b) IndexError c) ValueError d) TypeError
b) yes, like except [TypeError, SyntaxError] 24. What will be the output of the following Python code if the input
c) no d) none of the mentioned entered is 6?
17. What will be the output of the following Python code? valid = False
def foo(): while not valid:
try: try:
print(1) n=int(input("Enter a number"))
finally: while n%2==0:
print(2) print("Bye")
foo() valid = True
a) 1 2 b) 1 c) 2 d) none of the mentioned except ValueError:
18. Which of the following is not an exception handling keyword in print("Invalid")
Python? a) Bye (printed once) b) No output c) Invalid (printed once)
a) try b) except c) exception d) finaly d) Bye (printed infinite number of times)
19. a=False 25. Which of the following is not a standard exception in Python?
while not a: a) NameError b) IOError c) AssignmentError d) ValueError
26. An exception is ____________. 33. What is the error raised when a built-in function is inputted with invalid
a) an object b) a special function data type values?
c) a standard module d) a module a) ValueError b) TypeError C) SytaxError D) AttributeError
27. _________ exceptions are raised as a result of an error in opening a 34. Find the output of the below code. name = „Python‟
particular file. try:
a) ValueError b) TypeError c) ImportError d) IOError print(Name,end=”)
except:
28. print(“Exception”,end=”)
print(“!”)
a) Python! b) PythonException
c) Exception! d) PythonException!
35. Eithr ______ or _________ block is needed to execute a try block.
36. Write a Python program that prompts the user to input an integer and
29. raises a ValueError exception if the input is not a valid integer.
37. Write a Python program that executes an operation on a list and handles
an IndexError exception if the index is out of range.
38. Write a Python program that executes division and handles an
30. ArithmeticError exception if there is an arithmetic error.
39. Write a Python program to handle a ZeroDivisionError exception when
dividing a number by zero.
40. Consider the code given below and fill in the blanks.
print (" Learning Exceptions...")
try:
num1= int(input ("Enter the first number"))
num2=int(input("Enter the second number"))
quotient=(num1/num2)
print ("Both the numbers entered were correct")
except _____________: # to enter only integers
print (" Please enter only numbers")
31. except ____________: # Denominator should not be zero
print(" Number 2 should not be zero")
else:
print(" Great .. you are a good programmer")
32.
___________: # to be executed at the end
print(" JOB OVER... GO GET SOME REST")

You might also like