Python Programming (2180711) MCQs

MCQs of Testing, Debugging, Exceptions and Assertions

Showing 1 to 10 out of 18 Questions
1.
How many except statements can a try-except block have?
(a) zero
(b) one
(c) more than one
(d) more than zero
Answer:

Option (d)

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

Option (c)

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

Option (b)

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

Option (d)

5.
Can one block of except statements handle multiple exceptions?
(a) yes, like except TypeError, SyntaxError [,…]
(b) yes, like except [TypeError, SyntaxError]
(c) no
(d) none of the mentioned
Answer:

Option (a)

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

Option (d)

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

Option (b)

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

Option (b)

9.
The following Python code will result in an error if the input value is entered as -5.
assert False, 'Spanish'
(a) TRUE
(b) FALSE
Answer:

Option (a)

10.
Which of the following is not an exception handling keyword in Python?
(a) try
(b) except
(c) accept
(d) finally
Answer:

Option (c)

Showing 1 to 10 out of 18 Questions