250+ TOP MCQs on Python Exception Handling and Answers

Python Multiple Choice Questions on “Exception Handling – 3”.

1. What happens if the file is not found in the following Python code?

a=False
while not a:
    try:
        f_n = input("Enter file name")
        i_f = open(f_n, 'r')
    except:
        print("Input file not found")

a) No error
b) Assertion error
c) Input output error
d) Name error
Answer: a
Clarification: In the code shown above, if the input file in not found, then the statement: “Input file not found” is printed on the screen. The user is then prompted to reenter the file name. Error is not thrown.

2. What will be the output of the following Python code?

a) NameError
b) ValueError
c) IndexError
d) TypeError
Answer: c
Clarification: The snippet of code shown above throws an index error. This is because the index of the list given in the code, that is, 3 is out of range. The maximum index of this list is 2.

3. What will be the output of the following Python code?

a) IndexError
b) NameError
c) TypeError
d) ValeError
Answer: b
Clarification: The expression shown above results in a name error. This is because the name ‘t’ is not defined.

4. What will be the output of the following Python code, if the time module has already been imported?

a) NameError
b) IndexError
c) ValueError
d) TypeError
Answer: d
Clarification: The line of code shown above will result in a type error. This is because the operand ‘+’ is not supported when we combine the data types ‘int’ and ‘str’. Sine this is exactly what we have done in the code shown above, a type error is thrown.

5. What will be the output of the following Python code?

a) ImportError
b) ValueError
c) TypeError
d) NameError
Answer: b
Clarification: The snippet of code shown above results in a value error. This is because there is an invalid literal for int() with base 10: ’65.43’.

6. Compare the following two Python codes shown below and state the output if the input entered in each case is -6?

CODE 1
import math
num=int(input("Enter a number of whose factorial you want to find"))
print(math.factorial(num))
 
CODE 2
num=int(input("Enter a number of whose factorial you want to find"))
print(math.factorial(num))

a) ValueError, NameError
b) AttributeError, ValueError
c) NameError, TypeError
d) TypeError, ValueError
Answer: a
Clarification: The first code results in a ValueError. This is because when we enter the input as -6, we are trying to find the factorial of a negative number, which is not possible. The second code results in a NameError. This is because we have not imported the math module. Hence the name ‘math’ is undefined.

7. What will be the output of the following Python code?

def getMonth(m):
    if m<1 or m>12:
        raise ValueError("Invalid")
    print(m)
getMonth(6)

a) ValueError
b) Invalid
c) 6
d) ValueError(“Invalid”)
Answer: c
Clarification: In the code shown above, since the value passed as an argument to the function is between 1 and 12 (both included), hence the output is the value itself, that is 6. If the value had been above 12 and less than 1, a ValueError would have been thrown.

8. What will be the output of the following Python code if the input entered is 6?

valid = False
while not valid:
    try:
        n=int(input("Enter a number"))
        while n%2==0:
            print("Bye")
        valid = True
    except ValueError:
        print("Invalid")

a) Bye (printed once)
b) No output
c) Invalid (printed once)
d) Bye (printed infinite number of times)
Answer: d
Clarification: The code shown above results in the word “Bye” being printed infinite number of times. This is because an even number has been given as input. If an odd number had been given as input, then there would have been no output.

9. Identify the type of error in the following Python codes?

Print(“Good Morning”)
print(“Good night)

a) Syntax, Syntax
b) Semantic, Syntax
c) Semantic, Semantic
d) Syntax, Semantic
Answer: b
Clarification: The first code shows an error detected during execution. This might occur occasionally. The second line of code represents a syntax error. When there is deviation from the rules of a language, a syntax error is thrown.

10. Which of the following statements is true?
a) The standard exceptions are automatically imported into Python programs
b) All raised standard exceptions must be handled in Python
c) When there is a deviation from the rules of a programming language, a semantic error is thrown
d) If any exception is thrown in try block, else block is executed
Answer: a
Clarification: When any exception is thrown in try block, except block is executed. If exception in not thrown in try block, else block is executed. When there is a deviation from the rules of a programming language, a syntax error is thrown. The only true statement above is: The standard exceptions are automatically imported into Python programs.

11. Which of the following is not a standard exception in Python?
a) NameError
b) IOError
c) AssignmentError
d) ValueError
Answer: c
Clarification: NameError, IOError and ValueError are standard exceptions in Python whereas Assignment error is not a standard exception in Python.

12. Syntax errors are also known as parsing errors.
a) True
b) False
Answer: a
Clarification: Syntax errors are known as parsing errors. Syntax errors are raised when there is a deviation from the rules of a language. Hence the statement is true.

13. An exception is ____________
a) an object
b) a special function
c) a standard module
d) a module
Answer: a
Clarification: An exception is an object that is raised by a function signaling that an unexpected situation has occurred, that the function itself cannot handle.

14. _______________________ exceptions are raised as a result of an error in opening a particular file.
a) ValueError
b) TypeError
c) ImportError
d) IOError
Answer: d
Clarification: IOError exceptions are raised as a result of an error in opening or closing a particular file.

15. Which of the following blocks will be executed whether an exception is thrown or not?
a) except
b) else
c) finally
d) assert
Answer: c
Clarification: The statements in the finally block will always be executed, whether an exception is thrown or not. This clause is used to close the resources used in a code.

Leave a Reply

Your email address will not be published. Required fields are marked *