Error Handling and Exceptions # MCQs Practice set

Q.1 Which of the following is a correct way to handle exceptions in Python?

try: ... except: ...
handle: ... catch: ...
begin: ... rescue: ...
error: ... handle: ...
Explanation - In Python, exceptions are handled using the 'try' block followed by an 'except' block.
Correct answer is: try: ... except: ...

Q.2 What is raised when you try to divide a number by zero in Python?

ZeroDivisionError
ValueError
TypeError
ArithmeticOverflowError
Explanation - Python raises a ZeroDivisionError when division by zero occurs.
Correct answer is: ZeroDivisionError

Q.3 Which exception is raised when a file is not found?

FileNotFoundError
IOError
ImportError
EOFError
Explanation - FileNotFoundError is raised when trying to open a file that does not exist.
Correct answer is: FileNotFoundError

Q.4 What does the 'finally' block do in Python exception handling?

Executes only if there is an exception
Executes only if there is no exception
Executes regardless of exception occurrence
Prevents exceptions from occurring
Explanation - The 'finally' block is always executed, whether an exception occurs or not.
Correct answer is: Executes regardless of exception occurrence

Q.5 Which keyword is used to manually raise an exception in Python?

throw
raise
except
catch
Explanation - The 'raise' keyword is used to explicitly raise exceptions in Python.
Correct answer is: raise

Q.6 What type of error occurs when a variable is used before assignment?

NameError
TypeError
ValueError
IndexError
Explanation - NameError is raised when a local or global name is not found.
Correct answer is: NameError

Q.7 In Python, which exception is raised when invalid type operations are performed?

TypeError
ValueError
KeyError
RuntimeError
Explanation - TypeError occurs when an operation is applied to an object of inappropriate type.
Correct answer is: TypeError

Q.8 Which exception occurs when trying to access a list index that does not exist?

IndexError
KeyError
ValueError
TypeError
Explanation - IndexError is raised when a sequence subscript is out of range.
Correct answer is: IndexError

Q.9 What is the base class for all built-in exceptions in Python?

Exception
BaseException
Error
Throwable
Explanation - BaseException is the root class for all built-in exceptions in Python.
Correct answer is: BaseException

Q.10 Which exception is raised when the end of a file is reached unexpectedly?

EOFError
FileNotFoundError
IOError
StopIteration
Explanation - EOFError occurs when the input() function hits the end of file condition.
Correct answer is: EOFError

Q.11 Which exception is raised when a dictionary key is not found?

KeyError
IndexError
AttributeError
ValueError
Explanation - KeyError occurs when trying to access a dictionary with a key that does not exist.
Correct answer is: KeyError

Q.12 What does the 'else' block in try-except-else do?

Executes if an exception occurs
Executes if no exception occurs
Executes regardless of exception
Prevents exceptions
Explanation - The 'else' block executes only if no exceptions are raised in the 'try' block.
Correct answer is: Executes if no exception occurs

Q.13 Which of the following is a syntax error in Python?

x == 5
if x = 5:
print(x)
y = x + 2
Explanation - In Python, '=' is assignment, '==' is comparison. 'if x = 5:' is a syntax error.
Correct answer is: if x = 5:

Q.14 Which exception is raised when importing a module that does not exist?

ImportError
ModuleNotFoundError
KeyError
NameError
Explanation - ModuleNotFoundError is raised when an imported module cannot be found.
Correct answer is: ModuleNotFoundError

Q.15 What exception occurs when an operation is performed on incompatible numeric types?

TypeError
ValueError
ZeroDivisionError
ArithmeticError
Explanation - TypeError occurs when an operation is applied to an object of inappropriate type, like adding string and integer.
Correct answer is: TypeError

Q.16 Which exception occurs when a key or index is out of bounds in a sequence or mapping?

LookupError
ValueError
TypeError
SyntaxError
Explanation - LookupError is the base class for KeyError and IndexError, used for invalid lookups.
Correct answer is: LookupError

Q.17 What will happen if an exception is not handled in Python?

Program will terminate
Program will continue normally
Exception will be ignored
Python will automatically fix it
Explanation - Unhandled exceptions cause the Python program to terminate and print a traceback.
Correct answer is: Program will terminate

Q.18 Which exception is raised when an operation or function receives an argument of correct type but inappropriate value?

ValueError
TypeError
NameError
KeyError
Explanation - ValueError occurs when the argument type is correct but the value is not suitable for the operation.
Correct answer is: ValueError

Q.19 Which built-in function can be used to raise custom exceptions?

raise()
throw()
assert()
error()
Explanation - 'raise' is used in Python to trigger exceptions intentionally.
Correct answer is: raise()

Q.20 What kind of exception is SyntaxError?

Compile-time exception
Run-time exception
Logical exception
Semantic exception
Explanation - SyntaxError occurs when the parser encounters invalid syntax, which is detected before runtime.
Correct answer is: Compile-time exception

Q.21 Which Python exception is raised by the 'assert' statement if a condition is false?

AssertionError
ValueError
RuntimeError
TypeError
Explanation - AssertionError is raised when an assert statement fails.
Correct answer is: AssertionError

Q.22 Which is the correct order of blocks in Python exception handling?

try → except → else → finally
try → else → except → finally
try → finally → except → else
except → try → else → finally
Explanation - The correct order is: try first, except to catch exceptions, optional else if no exception, finally always.
Correct answer is: try → except → else → finally

Q.23 What exception is raised when you try to access an attribute that does not exist?

AttributeError
KeyError
TypeError
NameError
Explanation - AttributeError occurs when an invalid attribute reference is made on an object.
Correct answer is: AttributeError

Q.24 Which statement is used to define a custom exception in Python?

class MyError(Exception): pass
def MyError(): pass
exception MyError(): pass
raise MyError()
Explanation - Custom exceptions are defined by creating a new class that inherits from Exception.
Correct answer is: class MyError(Exception): pass

Q.25 Which exception is used when an operation exceeds the allowed numeric limits?

OverflowError
ArithmeticError
TypeError
ValueError
Explanation - OverflowError occurs when calculations exceed the maximum representable numeric value.
Correct answer is: OverflowError