Basic Python/Exception Handling Questions
from CUET 2023-2024 Papers (with answers)
2023 - Shift 1
1. Which of the following is not a valid exception in Python?
a) ImportError
b) SyntaxError
c) NameError
d) ExceptionError
2. What will be the output of the following code?
try:
print(x)
except NameError:
print("Variable x is not defined")
a) Variable x is not defined
b) Syntax Error
c) NameError
d) No output
3. In Python, which statement is used to handle the exception?
a) catch
b) finally
c) try
d) except
2023 - Shift 2
4. What will be the output of the following code?
try:
print(10/0)
except ZeroDivisionError:
print("Division by zero not allowed")
a) 0
b) Error
c) Division by zero not allowed
d) No output
5. Which of the following is raised when a function receives an argument of correct type
but improper value?
a) TypeError
b) ValueError
c) NameError
d) SyntaxError
6. In Python, which of the following is not a valid variable name?
a) my_var
b) _myvar
c) 1myvar
d) myVar
7. What will be the output of the following Python code?
print(True and False or True)
a) True
b) False
c) Error
d) None
2023 - Shift 3
8. Which of the following is not a compile-time error in Python?
a) SyntaxError
b) IndentationError
c) NameError
d) ImportError
9. Which of the following is not a runtime exception in Python?
a) ZeroDivisionError
b) SyntaxError
c) OverflowError
d) IndexError
10. Which exception will be raised when a module is not found in Python?
a) ModuleNotFoundError
b) ImportError
c) NameError
d) ValueError
11. Which of the following exceptions is raised when an index is out of range?
a) TypeError
b) IndexError
c) NameError
d) ValueError
12. In Python, which statement is used to handle the exception?
a) catch
b) try
c) finally
d) throw
13. In Python exception handling, which block executes whether an exception occurs or
not?
a) try
b) except
c) finally
d) else
14. Which of the following exceptions will be raised if you try to open a file that does not
exist?
a) IOError
b) FileNotFoundError
c) FileError
d) OSError
15. Which of the following is not a valid exception in Python?
a) SyntaxError
b) TypeError
c) NameError
d) ErrorException
16. In Python, which statement is used to raise an exception?
a) throw
b) raise
c) except
d) error
17. If both an exception and finally block execute a return statement, which one will take
precedence?
a) except block's return
b) finally block's return
c) Both will execute
d) It will cause an error
2024 Paper
18. Which exception is raised when a file that needs to be opened cannot be found?
a) IOError
b) FileNotFoundError
c) FileOpenError
d) NameError
19. In Python, what will the following code do?
try:
f = open("[Link]", "r")
print([Link]())
except FileNotFoundError: print("File not found")
finally:
print("Operation complete")
a) Print file content or "File not found", then "Operation complete"
b) Print file content only if file exists
c) Print "Operation complete" only
d) Raise an error regardless of file existence
20.Which of the following is not a valid exception handling block in Python?
a) try-except
b) try-finally
c) try-catch
d) try-except-finally
21. In Python, what is the purpose of the raise statement?
a) To create a custom exception
b) To throw an exception
c) To handle an exception
d) To define a new exception class
22.What will be the output of the following code?
x = 10
y = "20"
print(str(x) + y)
a) 30
b) 1020
c) "1020"
d) Error
23.Which of the following is the correct way to convert a string to an integer in Python?
a) int(str)
b) integer(str)
c) [Link]()
d) convert(str, int)
24.What will be the result of the expression: 3 * 1 ** 3?
a) 9
b) 3
c) 27
d) 1
25.What is the output of the following code?
s = "Python Programming"
print(s[7:])
a) Program
b) Programming
c) Python
d) Py
ANSWERS
1. ExceptionError - Not a valid built-in exception in Python
2. Variable x is not defined - When using an undefined variable, Python raises
NameError with this message.
3. except - Python uses "except" keyword to handle exceptions, not "catch"
4. Division by zero not allowed - Output from ZeroDivisionError handler .
5. ValueError - Raised when a function receives correct type but improper value .
6. 1myvar - Variable names can't start with digits in Python (violates naming rules).
7. True - Boolean expression evaluation follows operator precedence (True and False =
False, False or True = True). And precedence is more than or
8. NameError - Raised at runtime when variables aren't defined, not during compilation.
Compile error are the error raised when the program is compiled and runtime are those
which occur during execution of program
9. SyntaxError - A compile-time error when code violates Python grammar rules .
10. ImportError - Raised when import statement can't find the module .
11. IndexError - Occurs when accessing index outside valid range .
12. try - Contains code that might raise exceptions
13. finally - Block executes regardless of exception occurrence
14. FileNotFoundError - Raised when opening a non-existent file
15. ErrorException - Not a valid Python exception type
16. raise - Keyword used to throw exceptions in Python
17. finally block's return - Takes precedence over except block's return.
18. FileNotFoundError - Raised when file to be opened doesn't exist.
19. Print content/'File not found', then 'Operation complete' - try/except/finally flow
execution.
20.try-catch - Python uses try-except, not try-catch
21. To throw an exception - Primary purpose of raise statement .
22.1020 - String concatenation joins "10" and "20"
23.int(str) - Correct function to convert string to integer.
24.3 - Expression 31**3 equals 31=3 due to operator precedence.
25.Programming - String slice s[7:] returns all characters from index 7 onward