Basics
1. Which of the following is NOT an
error type in Python?
a) Syntax
error
b) Runtime
error
c) Logical
error
d) Debugging
error
Answer: d)
2. What is an exception in Python?
a) A function
that fixes errors
b) A Python
object that represents an error
c) A warning
generated by the compiler
d) A syntax
checking method
Answer: b)
3. Syntax errors in Python are also
called:
a) Parsing
errors
b) Runtime
errors
c) Logical
errors
d) System
errors
Answer: a)
4. Which error prevents the program
from running until it is fixed?
a) Runtime
error
b) Syntax
error
c) Logical
error
d) Exception
error
Answer: b)
5. Which of the following can raise
exceptions in Python?
a) Division by
zero
b) Accessing
invalid index
c) Using
undefined variables
d) All of the
above
Answer: d)
Built-in Exceptions
6. Which exception is raised when a
denominator is zero?
a) ValueError
b) ZeroDivisionError
c) TypeError
d) NameError
Answer: b)
7. Which of the following exceptions
is raised when a variable is not defined? a) SyntaxError
b) NameError
c) ValueError
d) TypeError
Answer: b)
8. ImportError occurs when:
a) Wrong
number of arguments passed
b) File cannot
be opened
c) Requested
module is not found
d) Division by
zero occurs
Answer: c)
9. Which error is raised when
indentation is incorrect in Python code?
a) TypeError
b) IndentationError
c) NameError
d) ValueError
Answer: b)
10. EOFError occurs when:
a) A file
cannot be opened
b) input()
reaches end of file without reading data
c) Wrong
function argument
d) Keyboard
interrupt
Answer: b) Answer: b) Answer: a)
Raising Exceptions
11. Which statement is used to raise
exceptions in Python?
a) throw
14. Which of
the following is NOT true about raise?
a) It can
raise built-in exceptions
b) It can
raise user-defined exceptions
c) It resumes
execution of the current block
d) It
interrupts the normal flow of the program Answer:
c)
15. If an
exception is raised using raise, what
happens to the following statements in the block?
a) They are
skipped
b) They
continue execution
c) They are
executed only if no exception occurs
d) They are
executed with a warning
Answer: a)
Handling Exceptions
16. Which keyword is used to start a
block of code that may cause an exception? a) raise
b) assert
c) try
d) except
Answer: c)
17. Exception handling in Python is
done using:
a) try...except
b) try...except...else
c) try...except...finally
d) All of the
above
Answer: d)
18. What happens if no exception occurs
inside try block?
a) except
block is executed
b) else block
is executed (if present)
c) finally
block is skipped
d) Program
terminates abnormally
Answer: b)
19. Which block is always executed
irrespective of exception occurrence? a) try
b) except
c) else
d) finally
Answer: d)
20. If no matching exception is found
in except blocks, what happens?
a) Program
continues normally
b) Exception
is ignored
c) Program
terminates
d) Control
goes to the else block
Answer: c)
Multiple Excepts and Flow
21. Which is correct syntax to handle
multiple exceptions?
a) Multiple
try blocks
b) Multiple
except blocks for one try block
c) Nested
finally blocks
d) Multiple
else blocks
Answer: b)
22. What will happen if you use an
except block without mentioning exception type? a) Error
a) Variables and their values
Answer: b)
25. Which of the following blocks can
appear only once in exception handling? a) try
b) except
c) else
d) finally
Answer: a),
c), d) (all must appear at most once)
Applied Questions
26. What will happen if denominator entered is zero in the
following code?
try:
print(10/0) except ZeroDivisionError: print("Error handled") a) Program
terminates
b) "Error
handled" is displayed
c) No output
d) Runtime
error occurs
Answer: b)
27. Which exception is raised in this code?
numbers
= [1,2,3] print(numbers[10]) a) ValueError
Answer:
b)
a) Program
ignores exception
b) finally
block executes, then exception re-raised
c) Program
terminates without error
d) else block
executes
Answer: b)
30. Which of the following is NOT a
built-in exception?
a) MemoryError
b) NameError
c) CompileError
d) EOFError
Answer: c)
2
Marks Questions (10)
1.
Q: Differentiate between syntax errors and exceptions in
Python.
A: Syntax errors occur when the code violates Python’s syntax rules;
exceptions occur during program execution even if syntax is correct.
2.
Q: Explain the purpose of the raise statement in Python.
A: The raise
statement is used to manually throw an exception, interrupting the normal
program flow and transferring control to the exception handler.
3.
Q: What is the difference between assert and raise
in Python?
A: assert
tests a condition and raises an AssertionError if false; raise explicitly throws any specified exception.
4.
Q: Name and explain any four built-in
exceptions in Python.
A:
- ZeroDivisionError:
Raised when dividing by zero.
- ValueError:
Raised when an operation receives an inappropriate value.
- NameError:
Raised when a variable is not defined.
- IOError:
Raised when a file cannot be opened.
5.
Q: What is the purpose of a finally block?
A: The finally block contains code that is always executed, regardless of
whether an exception occurred or not, commonly used for resource cleanup.
6.
Q: Explain the difference between try…except and try…except…else.
A: In try…except, except runs only if an exception occurs. In try…except…else, the else block runs only if no exception occurs.
7.
Q: What is meant by catching an exception?
A: Catching an exception means executing code in an except block
designed to handle a particular exception raised in the try block.
8.
Q: Give an example where IndexError can occur.
A: Accessing mylist[5] when mylist
has only 3 elements will raise IndexError.
9.
Q: When is a ValueError raised in Python?
A: When a function receives an argument of correct type but inappropriate value, e.g., int("abc").
10.
Q: Explain the process of exception handling in Python.
A: The interpreter creates an exception object, searches for a suitable
handler in the call stack, and executes the handler code if found. If not
found, the program stops.
3
Marks Questions (10)
1.
Q: Write a Python program to accept two numbers and display
their quotient. Raise a ZeroDivisionError if the denominator is zero.
A:
num1
= int(input("Enter numerator: "))
num2
= int(input("Enter denominator: "))
if
num2 == 0:
raise ZeroDivisionError("Denominator
cannot be zero")
print("Quotient:",
num1 / num2)
2.
Q: Write a program using assert to check that a number is positive before calculating its
square.
A:
def
square(number):
assert number >= 0, "OOPS...
Negative Number"
return number * number
print(square(10))
3.
Q: Explain the purpose of multiple except blocks in Python
with an example.
A: Multiple except blocks allow handling different exceptions
separately.
Example:
try:
x = int(input("Enter a number:
"))
y = int(input("Enter another number:
"))
print(x / y)
except
ZeroDivisionError:
print("Cannot divide by zero")
except
ValueError:
print("Invalid input")
4.
Q: What is the role of the call stack in exception handling?
A: The call stack maintains the sequence of function calls and helps the
runtime system search for an appropriate exception handler.
5.
Q: Write a program using try-except-else to divide two
numbers.
A:
try:
x = int(input("Enter numerator:
"))
y = int(input("Enter denominator:
"))
result = x / y
except
ZeroDivisionError:
print("Cannot divide by zero")
except
ValueError:
print("Enter valid numbers")
else:
print("Quotient is", result)
6.
Q: Explain the difference between user-defined and built-in
exceptions.
A: Built-in exceptions are predefined by Python (e.g.,
ZeroDivisionError); user-defined exceptions are custom exceptions created by
programmers for specific needs.
7.
Q: Give an example where IOError occurs.
A: Trying to open a non-existent file:
f
= open("nofile.txt", "r")
# Raises IOError
8.
Q: What happens if an exception occurs but no handler is defined?
A: The program stops and Python displays a traceback with the exception
details.
9.
Q: Explain how finally helps in recovering from exceptions.
A: Finally block executes regardless of exception occurrence, ensuring
cleanup actions like closing files before the exception propagates further.
10.
Q: Write a Python program to catch any exception without
specifying its name.
A:
try:
x = int(input("Enter number: "))
print(10 / x)
except:
print("Some exception occurred")
5
Marks Questions (5)
1.
Q: Write a Python program to handle multiple exceptions
(ZeroDivisionError, ValueError, and any other) using try-except-else-finally.
A:
try:
num1 = int(input("Enter numerator:
"))
num2 = int(input("Enter denominator:
"))
result = num1 / num2
except
ZeroDivisionError:
print("Cannot divide by zero")
except
ValueError:
print("Enter valid numbers")
except:
print("Some unexpected exception
occurred")
else:
print("Quotient is", result)
finally:
print("Program execution
completed")
2.
Q: Explain raising, catching, and handling exceptions with an
example for each.
A:
- Raising:
raise ValueError("Invalid input") interrupts program flow.
- Catching:
Using except block to execute code when exception occurs.
- Handling:
Code in except block fixes or informs user.
Example:
try:
x = int(input("Enter number >0:
"))
if x <= 0:
raise ValueError("Number must be
positive")
except
ValueError as e:
print(e)
3.
Q: Create a program using assert to check positive numbers and
handle AssertionError using try-except.
A:
try:
num = int(input("Enter number:
"))
assert num > 0, "Number must be
positive"
except
AssertionError as e:
print(e)
else:
print("Square is", num*num)
4.
Q: Explain the flow of try…except…else…finally with a
diagrammatic description.
A: Flow:
- Try block executes co
de where exception may occur. - If exception occurs → except block executes.
- If no exception → else block executes.
- Finally block executes always.
(Diagram can be drawn as: try → except (if error) / else (if no error) → finally (always))
5.
Q: Write a Python program that opens a file, reads content,
and ensures the file is closed using finally. Handle any IOError.
A:
try:
f = open("example.txt",
"r")
data = f.read()
print(data)
except
IOError:
print("File not found or cannot be
opened")
finally:
f.close()
print("File closed successfully")
[KINDLY NOTE: CROSS CHECK ALL ANSWERS BEFORE USE WITH YOUR TEXT BOOK/LECTURER/ACCURACY OF ANSWERS MAY NOT BE 100% CORRECT AS THEY ARE EXTRACTED FROM AI TOOLS, QUANTITY OF ANSWER MAY NOT BE SUFFICIENT FOR QUESTIONS, THIS IS JUST FOR REFERENCE ]
Comments
Post a Comment