Python

Python

Exception Handling has 7 Questions
. How many except statements can a try-except block have?

zero
one
more than one
more than zero
When will the else part of try-except-else be executed?

always
when an exception occurs
when no exception occurs
when an exception occurs in to except block
Is the following code valid?
try:
    # Do something
except:
    # Do something
finally:
    # Do something

no, there is no such thing as finally
no, finally cannot be used with except
no, finally must come before except
yes
Can one block of except statements handle multiple exception?

yes, like except TypeError, SyntaxError [,...]
yes, like except [TypeError, SyntaxError]
no
none of the mentioned
When is the finally block executed?

when there is no exception
when there is an exception
only if some condition that has been specified is satisfied
always
What is the output of the following code?
def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)

1
2
3
error, there is more than one return statement in a single try-finally block
What is the output of the following code?
def foo():
    try:
        print(1)
    finally:
        print(2)
foo()


1 2
1
2
none of the mentioned