0% found this document useful (0 votes)
4 views9 pages

Python Exception Handling Quiz

The document discusses exception handling in Python, providing code examples and questions related to the behavior of try, except, else, and finally blocks. It includes multiple-choice questions with correct and incorrect answers regarding various types of errors and the functionality of exception handling constructs. The document serves as a quiz or educational material on Python's error handling mechanisms.

Uploaded by

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

Python Exception Handling Quiz

The document discusses exception handling in Python, providing code examples and questions related to the behavior of try, except, else, and finally blocks. It includes multiple-choice questions with correct and incorrect answers regarding various types of errors and the functionality of exception handling constructs. The document serves as a quiz or educational material on Python's error handling mechanisms.

Uploaded by

kvvgrvidyalaya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Exception Handling in Python

[Link] the following code:

Start=0
while True:
try:
Number=int(input("Enter Number"))
break
except ValueError:
Start=Start+1
print(Start )
print(Start)

What will be the output of the above Python code considering the following set of inputs ?
A3
3

11

10

01

error

[Link] will be the output of the following Python code?

try:

x = 10 / 2

except ZeroDivisionError:

print("Division by zero")

else:

print("No exception")

finally:

print("Finally block")

a. Division by zero
b. No exception

c. Division by zero, Finally block

d. No exception, Finally block


Incorrect
0/1 Points
[Link] is the output of the following code?

try:
print(1, end=" ")
except:
print(2, end=" ")
else:
print(3, end=" ")
finally:
print(4, end=" ")

(a) 1 2 3 4

(b) 1 3 4

(c) 1 4

(d) 4
Incorrect
0/1 Points
[Link] keyword is used to specify a generic exception handler in Python?

a. catch

b. except

c. all

d. finally
Correct
1/1 Points
[Link] is the purpose of the "else" block in Python's exception handling?

a. To specify the code that should always execute.

b. To handle exceptions and specify alternative code.

c. To define a block of code that may raise an exception.


d. To specify the code to execute if no exceptions occur.
Incorrect
0/1 Points
[Link] you attempt to perform an operation between two incompatible data types, which error
will you encounter?

a. ZeroDivisionError

b. TypeError

c. NameError

d. SyntaxError
Correct
1/1 Points
[Link] you try to access an element in a list using an index that is out of range, which error will
you encounter?

IndexError

NameError

SyntaxError

ValueError
Incorrect
0/1 Points
[Link] error is raised when you have a typo or a syntax mistake in your Python code?

a. NameError

b. TypeError

c. SyntaxError

d. ValueError
Correct
1/1 Points
[Link] does the "except:" (with a colon but without specifying an exception type) block in
Python's exception handling handle?

a. All exceptions

b. Only NameError exceptions


c. Only ZeroDivisionError exceptions

d. Only SyntaxError exceptions


Correct
1/1 Points
[Link] does the "finally" block in Python's exception handling ensure?

a. It ensures that the code within the "try" block is executed.

b. It ensures that the code within the "except" block is executed.

c. It ensures that the code within the "else" block is executed.

d. It ensures that the code within the "finally" block is executed.


Incorrect
0/1 Points
[Link] many "except" blocks can you have following a single "try" block in Python?

a. Only one

b. As many as needed

c. Two at most

d. Three at most
Incorrect
0/1 Points
[Link] Python, what follows the "except" keyword when handling exceptions?

a. The exception message

b. The error code

c. The type of exception

d. The line number of the exception


Correct
1/1 Points
[Link] error is raised when you attempt to call a function that does not exist in your code?

a. TypeError

b. NameError

c. IndexError
d. SyntaxError
Correct
1/1 Points
[Link] will be the output of the following Python code?

try:

x = int("five")

except ValueError:

print("Value error")

else:

print("No exception")

finally:

print("Finally block")

a. Value error

b. No exception

c. Value error, Finally block

d. No exception, Finally block


Incorrect
0/1 Points
[Link] happens if an exception is raised in the "try" block and there is no corresponding
"except" block to handle it?

a. The program crashes and exits.

b. The "else" block is executed.

c. The program proceeds without any errors.

d. The "finally" block is executed.


Incorrect
0/1 Points
[Link] you use a variable that has not been defined in your code, which error will be raised?

a. NameError
b. SyntaxError

c. ZeroDivisionError

d. IndexError
Incorrect
0/1 Points
[Link] of the following statements is true about the "else" statement in exception
handling?

(a) The "else" statement is always executed, regardless of whether an exception is raised.

(b) The "else" statement is only executed if an exception is not raised.

(c) The "else" statement is required in every exception handling block.

(d) The "else" statement cannot be used with the "finally" statement.
Incorrect
0/1 Points
[Link] is the primary purpose of the "try" statement in Python's exception handling?

a. To specify the code that should always execute.

b. To define a block of code that may raise an exception.

c. To handle exceptions and specify alternative code.

d. To indicate the end of the exception handling block.


Incorrect
0/1 Points
[Link] error is raised when you forget to close parentheses, quotation marks, or brackets
in your code?

a. SyntaxError

b. IndexError

c. TypeError

d. NameError
Incorrect
0/1 Points
[Link] will be the output of the following Python code?

try:
x=5/0

except ZeroDivisionError:

print("Division by zero")

else:

print("No exception")

finally:

print("Finally block")

a. Division by zero

b. No exception

c. Division by zero, Finally block

d. No exception, Finally block

[Link] is the difference between the except and finally statements?

(a) The except statement is used to handle exceptions, while the finally statement is always
executed, regardless of whether an exception occurs.

(b) The except statement is used to handle specific exceptions, while the finally statement is
used to handle general exceptions.

(c) The except statement is used to catch exceptions, while the finally statement is used to
prevent exceptions from occurring.

(d) None of the above.

[Link] error occurs when you attempt to perform an operation that is not supported by the
data types involved?

a. NameError

b. TypeError

c. IndexError

d. ZeroDivisionError
[Link] is the order in which the "try," "except," "else," and "finally" blocks are executed?
a. try, else, except, finally

b. try, except, else, finally

c. try, except, finally, else

d. try, finally, except, else

[Link] type of error occurs when you attempt to divide a number by zero in Python?

NameError

ZeroDivisionError

IndexError

TypeError

[Link] error occurs when you try to use a variable outside of its scope, such as in a function
where it is not defined?

a. NameError

b. TypeError

c. SyntaxError

d. IndexError

[Link] part of the "try" block is executed when an exception is raised?

a. The "try" suite

b. The first "except" block

c. The "else" suite

d. The "finally" suite

[Link] error occurs when you pass the wrong number of arguments to a function in Python?

a. TypeError
b. IndexError

c. NameError

d. SyntaxError

ANSWERS

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

A B B B D C A C A A B C B C A A B C C C

21 22 23 24 25 26 27

A B B B A B A

You might also like