Exception Handling in Python
I Select the correct answer from the choices given:
1) What is a syntax error in Python?
a) An error related to the logical flow of the program.
b) An error in the logic of the program
c) An error that occurs when the rules of the programming language are not followed.
d) An error that occurs only during runtime.
2) What type of error is raised when you forget to close a parentheses in Python?
a) SyntaxError
b) ValueError
c) TypeError
d) RuntimeError
3) What is another name for a syntax error in programming?
a) Runtime error
b) Parsing error
c) Logical error
d) Exception error
4) What happens when a syntax error is encountered in Python?
a) The program executes
b) The program stops execution until the error is fixed.
c) The program runs slower.
d) The interpreter tries to correct the error automatically.
5) Which of the following is an example of a syntax error in Python?
a) Using a variable without declaring it first.
b) Forgetting to close a parenthesis in a function call.
c) Using the wrong type of operator.
d) Calling a function with the wrong number of arguments.
6) How does Python handle a program with a syntax error?
a) Python shows a warning but continues execution.
b) Python shows an error message and stops execution until the error is fixed.
c) Python prompt user to correct interactively
d) Python ignores the error and runs the program.
7) Identify the error the following programming segment
If marks >20 :
print “Good Score”
a) RuntimeError
b) LogicalError
c) KeyboardError
d) SyntaxError
8) Which exception is raised when not followed rules of Python language?
a) IndentationError
b) SyntaxError
c) NameError
d) ValueError
9) Which of the following is a valid example of a syntax error in Python?
a) print(“Hello World)
b) if x = 5:
c) def my function():
d) All of these
10) In the following Python code, what is the syntax error?
if x = = 10
print(“x is 10”)
a) The colon (:) is missing before the print statement.
b) The condition has a missing parentheses.
c) The colon (:) is missing after the if condition.
d) The colon (:) is missing after the print statement.
11) What is an exception in programming?
a) A type of error that occurs during the execution of a program
b) A warning message displayed after execution.
c) A successful execution of a conditional statement
d) A special type of loop in programming
12) What is an exception in Python?
a) A Python object that represents a successful program execution
b) A Python object that represents an error
c) A built-in function used for debugging
d) A type of variable in Python
13) What happens when an error occurs during the execution of a Python program?
a) The program continues executing as normal
b) it automatically recompile the program
c) An exception is raised
d) The program runs a default error message and continues
14) Why should exceptions be handled in Python?
a) To ensure that errors are ignored
b) To make the program run faster
c) To prevent the program from terminating abnormally
d) To execute the program without error.
15) What does the keyword do in an exception block ?
a) It raises the exception.
b) It assigns the exception object to a variable.
c) It catches the exception and terminate the program
d) It creates a custom except for class
16) What is the effect of using try-except blocks in Python?
a) It prevents the program from terminating abruptly.
b) It helps in performing automatic type casting.
c) It can only handle syntax errors.
d) It optimizes the program execution speed.
17) What type of error does Python raise when a function receives an argument of the correct
type but inappropriate value?
a) KeyboardInterrupt
b) ValueError
c) TypeError
d) NameError
18) Which exception is raised when the file specified in a program statement cannot be opened ?
a) IOError
b) EOFError
c)IndexError
d) Value error
19) Which exception is raised when the requested module definition is not found ?
a) FileNotFoundError
b) ImportError
c) SyntaxError
d) Module Not Found
20) Which exception is raised when the end of file condition is reached without reading any data
by input()?
a) EOF error
b) Import Error
c) Syntax error
d) Value error
21) Which exception is raised when a division by zero operation is performed?
a) ZeroError
b) TypeError
c) DivisionError
d)ZeroDivisionError
22) Which exception is raised when an index is out of range in a sequence?
a) KeyError
b) ValueError
c) IndexError
d) TypeError
23) Which exception is raised when a local or global variable name is not defined?
a) Import Error
b) NameError
c) Syntax error
d) Value Error
24) Which exception is raised due to incorrect indentation in the program code ?
a) ImportError
b)AssertionError
c) IndexError
d) IndentationError
25) Which exception is raised when an operation or function is applied to an object of
inappropriate data type?
a) TypeError
b) NameError
c) OverflowError
d) SyntaxError
26) Which built-in exception is raised when a file is not found?
a) FileNotFoundError
b) IOError
c) NameError
d) TypeError
27) What is the purpose of the raise statement in Python?
a) To raise the program’s execution to a higher point in the code.
b) To terminate the program
c) It can be used to throw an exception.
d) To end the program execution.
28) Which of the following is the correct syntax for raising an exception in Python?
a) throw Exception(‘Error message’)
b) raise Exception(‘optional argument’)
c) raise ‘Error message’
d) raise exception(‘Error message’
29) What is the primary purpose of the assert statement in Python?
a) To catch exceptions during program execution.
b) To define functions for debugging.
c) To test if a condition is true and raise an exception if it is false.
d) To handle errors in user input.
30) Which statement is used in python to raise an exception manually ?
a) assert
b) raise
c) throw
d) catch
31) Which of the following is the correct syntax for using an assert statement?
a) assert condition:
b) assert expression[arguments]
c) assert(condition)
d) assert: condition
32) What type of exception is raised when an assertion fails in Python?
a) TypeError
b) ValueError
c) IndexError
d) AssertionError
33) What is the primary purpose of exception handling in programming?
a) To make the code run faster
b) To manage runtime errors gracefully
c) To make code more complex
d) To reduce the size of the program
34) What is the main reason for using exception handling in programming?
a) To automatically fix errors
b) To manage unexpected errors and maintain program flow
c) To prevent syntax errors
d) To terminate program abruptly
35) What does the Python interpreter do when an error occurs during program execution?
a) It stops the program without any notification
b) It creates an exception object
c) It deletes all variables
d) It restarts the program
36) What is the process called when an exception object is created and handed over to the
runtime system for handling?
a) Handling an exception
b) Catching an exception
c) Throwing an exception
d) Ignoring an exception
37) What does the runtime system search for when an exception is raised?
a) The variable that caused the exception
b) The source code file where the exception occurred
c) A block of code called the exception handler that can handle the exception
d) The compiler directives related to the exception
38) What is the name of the block of code that handles exceptions raised during program
execution?
a) Error block
b) Catch block
c) Exception handler
d) Runtime block
39) Which of the following statements is true about using multiple except blocks in Python?
a) Only one except block is allowed per try block.
b) Multiple except blocks can be used to handle different exceptions separately
c) Multiple except blocks can be used, but only the last one is executed.
d) All except blocks are executed in sequence when an error occurs.
40) What will happen if an exception is raised inside the else block of a try-except-else structure?
a) The exception will be caught by the except block.
b) The program will terminate immediately.
c) The exception will be ignored, and the code will continue execution.
d) The exception will not be caught and will propagate outside the block.
41) Which of the following is true about the else block in Python exception handling?
a) It is executed if an exception is raised in the try block
b) It is executed only if no exception occurs in the try block
c) It is always executed, regardless of exceptions
d) it is used to define an alternate block
42) Which Python keyword is used to handle exceptions?
a) finally
b) identifier
c) catches
d) except
43) Which of the following keywords are not specific to exception handling?
a) Try
b) except
c) else
d) finally
44) What will happen if a try block contains a raise statement but there is no corresponding
except block?
a) The program will ignore the raised exception and continue.
b) The program will terminate with a traceback error.
c) The program will automatically catch the exception and continue.
d) The program will raise a new exception automatically.
45) What happens if an exception is raised inside a try block, but no except block is present to
catch it?
a) It call the else block
b) The program terminates with an error
c) The exception is caught by the raise statement
d) The program moves to the else block
46) What is the purpose of the else block in a try-except-else statement?
a) To handle exceptions that occur in the try block.
b) To run code when no exception occurs in the try block.
c) To execute code after the exception is raised.
d) To finalize code after all exceptions are handled.
47) Which of the following is used to handle exceptions in Python?
a) catch
b) exception
c) try - except
d) throw
48) In which block is the exception actually thrown?
a) try
b) catch
c) finally
d) exception
49) What will happen if an exception is raised but not caught in a try-except block?
a) The program will continue without interruption.
b) The program will terminate and display the traceback.
c) The program will ignore the exception and continue execution.
d) The program will prompt the user to handle the exception.
50) Which of the following will be executed if an exception is thrown and not caught?
a) The program terminates immediately
b) The else block executes
c) The finally block executes
d) Program continuous with next statement
51) Which of the following statements about the finally block is true?
a) It will be executed only if an exception is raised.
b) It will not be executed if an exception is raised.
c) after catch and try block,It will be executed whether or not an exception is raised.
d) It will not be executed if there is no exception.
52) Which of the following is true about exception handling?
a) It is only useful for debugging purposes
b) It makes the program slower by introducing additional checks
c) It allows the program to continue execution even when an error occurs
d) It is not necessary in programs with few errors
53) What is the purpose of a finally block in exception handling? (E)
a) It is used to define the exception
b)It always executes, regardless of whether an exception was thrown or not
c) It prevents the exception from being thrown
d) It rethrows the exception after handling
54) What is the role of the finally block in exception handling? (A)
a) To execute code after the exception is caught, regardless of whether an exception
occurred
b) To throw exceptions
c) To catch exceptions
d) To terminate the program
55) What is the primary purpose of the finally clause in Python? (A)
a) To handle specific exceptions
b) To catch all exceptions automatically
c) To ensure that code runs regardless of whether an exception occurs
d) To define the starting point of the program
56) What happens if an exception is raised in the finally block?
a) The exception is ignored.
b) The exception is logged for further analysis.
c) The exception is re-raised and the program terminates.
d) The exception is caught in the except block.
57) Which keyword is used to raise (throw) an exception in Python?
a) catch
b) throw
c) raise
d) except
58) What happens when an exception is raised in Python?
a) The program immediately terminates
b) The exception is passed silently
c) The exception is propagated and can be caught in a try-except block
d) The program prints a debug message and continues execution
59) Which of the following statements is true about the raise keyword?
a) It can only be used within a function
b) It can be used to throw both predefined and custom exceptions
c) It is used to handle exceptions
d) It is used to catch exceptions
60) If an exception is raised and not caught within the current function, what happens?
a) The program terminates immediately
b) The exception is passed to the next function call in the call stack
c) The exception is silently ignored
d) The program automatically corrects the error
61) Assertion (A): Zero Division Error is raised when a number is divided by zero. (D)
Reason (R): Division by zero is undefined in mathematics and therefore not allowed in Python.
a) Both A and R are true, but R is not the correct explanation of A
b) A is true, but R is false
c) A is false, but R is true
d) Both A and R are true, and R is the correct explanation of A
62) Assertion(A):TypeError is raised when an operator is supplied with a value of incorrect data
type.
Reason (R): Trying to concatenate a string and an integer without conversion causes a
TypeError.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is true, but R is false
d) A is false, but R is true
63) Assertion (A): ValueError is raised when a function receives a value of the correct data type
but inappropriate value.
Reason (R): Converting a non-numeric string to an integer using int("abc") raises a ValueError.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is true, but R is false
d) A is false, but R is true
64) Assertion (A):IndexError occurs when trying to access an index that is out of the range of a
list.
Reason (R): Lists in Python start indexing from 1.
a) Both A and R are true, and R is the correct explanation of A)
b) Both A and R are true, but R is not the correct explanation of A)
c) A is true, but R is false.
d) A is false, but R is true.
65) Assertion (A): Each time an error is detected in a program, the Python interpreter raises an
exception.
Reason (R): Exception handlers are designed to execute when a specific exception is raised.
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.
66) Assertion (A): Programmers can only raise exceptions using the raise statement in Python.
Reason (R): The assert statement can also be used to forcefully raise exceptions in Python. (D)
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is false, but R is true.
d) A is true, but R is false.
67) Assertion (A): The raise statement can be used to throw an exception in Python.
Reason (R): The raise statement is used only for handling exceptions that are caught by the try-
except block.
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.
68) Assertion (A): An assert statement in Python is used to test an expression in the program
code. If the result is false, an AssertionError is raised.
Reason (R): The assert statement is generally used at the beginning of a function or after a
function call to check for valid input.
a) Both Assertion and Reason are true, and Reason is the correct explanation of Assertion.
b) Both Assertion and Reason are true, but Reason is not the correct explanation of Assertion.
c) Assertion is true, but Reason is false.
d) Assertion is false, but Reason is true.
69) Assertion (A):Each and every exception has to be handled by the programmer to avoid the
program from crashing abruptly.
Reason (R): This is done by writing additional code in a program to give proper messages
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.
70) Assertion (A): In Python, the finally block is always executed ,whether or not an exception is
raised in the try block.
Reason (R): The finally clause ensures files are properly closed and must follow all except and
else blocks in a try statement.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is true
d) A is false, but R is true
71)Assertion(A): in python ,the else block in a try ..except ..else construct is executed only when
no exception is raised in the try block.
Reason(R): the except blocks runs only if an exception occurs in the try block;otherwise ,the
control moves to the else block if it exists
a)Both A and R are true, and R is the correct explanation of A
b)Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is true
d)A is false, but R is true
72)In python ,what is the purpose of he else block in exception handling?
73)a)to catch exceptions
b)except Exception1 and Exception2
c)except{Exception1,Exception2}
c)except[Exception1,Exception2}