Python Programming Multiple Choice Question -
Exception Handling
This section focuses on the "Exception Handling" in Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which block lets you test a block of code for errors?
A. try
B. except
C. finally
D. None of the above
View Answer
Ans : A
Explanation: The try block lets you test a block of code for errors.
2. What will be output for the folllowing code?
try:
print(x)
except:
print(""An exception occurred"")
A. x
B. An exception occurred
C. Error
D. None of the above
View Answer
Ans : B
Explanation: An exception occurred be the output for the followinng code because the try block will generate an error, because x is not defined.
3. What will be output for the folllowing code?
x = ""hello""
if not type(x) is int:
raise TypeError(""Only integers are allowed"")
A. hello
B. garbage value
C. Only integers are allowed
D. Error
View Answer
Ans : C
Explanation: TypeError: Only integers are allowed
4. What will be output for the folllowing code?
try:
f = open(""demofile.txt"")
f.write(""Lorum Ipsum"")
except:
print(""Something went wrong when writing to the file"")
finally:
f.close()
A. demofile.txt
B. Lorum Ipsum
C. Garbage value
D. Something went wrong when writing to the file
View Answer
Ans : D
Explanation: Something went wrong when writing to the file be output for the folllowing code.
5. Which exception raised when a calculation exceeds maximum limit for a numeric type?
A. StandardError
B. ArithmeticError
C. OverflowError
D. FloatingPointError
View Answer
Ans : C
Explanation: OverflowError : Raised when a calculation exceeds maximum limit for a numeric type
6. Which exception raised in case of failure of attribute reference or assignment?
A. AttributeError
B. EOFError
C. ImportError
D. AssertionError
View Answer
Ans : A
Explanation: AttributeError : Raised in case of failure of attribute reference or assignment.
7. How many except statements can a try-except block have?
A. 0
B. 1
C. more than one
D. more than zero
View Answer
Ans : D
Explanation: There has to be at least one except statement.
8. 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 above
View Answer
Ans : A
Explanation: Each type of exception can be specified directly. There is no need to put it in a list.
9. The following Python code will result in an error if the input value is entered as -5.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: The code shown above results in an assertion error.
10. What will be output for the folllowing code?
x=10
y=8
assert x>y, 'X too small
A. Assertion Error
B. 10 8
C. No output
D. 108
View Answer
Ans : C
Explanation: The code shown above results in an error if and only if xy, there is no error. Since there is no print statement, hence there is no output.
Discussion