Exception Handling
1.

How many except statements can a try-except block have?

A.  

zero

B.  

one

C.  

more than one

D.  

more than zero

2.

When will the else part of try-except-else be executed?

A.  

always

B.  

when an exception occurs

C.  

when no exception occurs

D.  

when an exception occurs in to except block

3.

Is the following Python code valid?

try:
    # Do something
except:
    # Do something
finally:
    # Do something
A.  

no, there is no such thing as finally

B.  

no, finally cannot be used with except

C.  

no, finally must come before except

D.  

yes

4.

Is the following Python code valid?

try:
    # Do something
except:
    # Do something
else:
    # Do something
A.  

no, there is no such thing as else

B.  

no, else cannot be used with except

C.  

no, else must come before except

D.  

yes

5.

Can one block of except statements handle multiple exception?

A.  

yes, like except TypeError, SyntaxError [,…]

B.  

yes, like except [TypeError, SyntaxError]

C.  

no

D.  

none of the mentioned

6.

When is the finally block executed?

A.  

when there is no exception

B.  

when there is an exception

C.  

only if some condition that has been specified is satisfied

D.  

always

7.

What will be the output of the following Python code?

def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)
A.  

1

B.  

2

C.  

3

D.  

error, there is more than one return statement in a single try-finally block

8.

What will be the output of the following Python code?

def foo():
    try:
        print(1)
    finally:
        print(2)
foo()
A.  

1 2

B.  

1

C.  

2

D.  

none of the mentioned

9.

What will be the output of the following Python code?

try:
    if '1' != 1:
        raise "someError"
    else:
        print("someError has not occurred")
except "someError":
    print ("someError has occurred")
A.  

someError has occurred

B.  

someError has not occurred

C.  

invalid code

D.  

none of the mentioned

10.

What happens when ‘1’ == 1 is executed?

A.  

we get a True

B.  

we get a False

C.  

an TypeError occurs

D.  

a ValueError occurs