Python MCQs on Exception Handling : SET 3

 

 

21. 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

Check Answer

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

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

lst = [1, 2, 3]

lst[3]

a) NameError

b) ValueError

c) IndexError

d) TypeError

Check Answer

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

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

t[5]

a) IndexError

b) NameError

c) TypeError

d) ValeError

Check Answer

Answer: b
The expression shown above results in a name error. This is because the name ‘t’ is not defined.

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

4 + ‘3’

a) NameError

b) IndexError

c) ValueError

d) TypeError

Check Answer

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

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

int(‘65.43’)

a) ImportError

b) ValueError

c) TypeError

d) NameError

Check Answer

Answer: b
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’.

26. 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

Check Answer

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

27. 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”)

Check Answer

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

28. 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)

Check Answer

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

29. 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

Check Answer

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

30. 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

Check Answer

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

31. Which of the following is not a standard exception in Python?

a) NameError

b) IOError

c) AssignmentError

d) ValueError

Check Answer

Answer: c
NameError, IOError and ValueError are standard exceptions in Python whereas Assignment error is not a standard exception in Python.

32. Syntax errors are also known as parsing errors.

a) True

b) False

Check Answer

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

33. An exception is ____________

a) an object

b) a special function

c) a standard module

d) a module

Check Answer

Answer: a
An exception is an object that is raised by a function signaling that an unexpected situation has occurred, that the function itself cannot handle.

34. _______________________ exceptions are raised as a result of an error in opening a particular file.

a) ValueError

b) TypeError

c) ImportError

d) IOError

Check Answer

Answer: d
IOError exceptions are raised as a result of an error in opening or closing a particular file.

35. Which of the following blocks will be executed whether an exception is thrown or not?

a) except

b) else

c) finally

d) assert

Check Answer

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

Page  1  2  3

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment