Exception Handling # MCQs Practice set

Q.1 What is an exception in programming?

A logical error in code
A signal that an error has occurred
A variable type
A compiler warning
Explanation - Exceptions represent runtime errors that disrupt normal program execution.
Correct answer is: A signal that an error has occurred

Q.2 Which block is used to handle exceptions in most programming languages?

if
catch
handle
switch
Explanation - Exceptions are caught using the catch block after a try block.
Correct answer is: catch

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

Executes only if no exception occurs
Executes always, regardless of exception
Stops program execution
Skips the catch block
Explanation - The finally block ensures cleanup code executes whether or not an exception occurs.
Correct answer is: Executes always, regardless of exception

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

throw
raise
error
except
Explanation - In Python, the 'raise' keyword is used to generate an exception.
Correct answer is: raise

Q.5 In Java, which class is the superclass of all exceptions?

Throwable
Error
Exception
RuntimeException
Explanation - The Throwable class is the parent of both Error and Exception classes in Java.
Correct answer is: Throwable

Q.6 What happens if an exception is not caught?

Program continues normally
Compiler fixes it automatically
Program terminates abnormally
It gets ignored
Explanation - Uncaught exceptions cause abnormal termination of the program.
Correct answer is: Program terminates abnormally

Q.7 Which of these is NOT a type of exception in Java?

Checked
Unchecked
Handled
Error
Explanation - Java exceptions are categorized as Checked, Unchecked, and Errors. 'Handled' is not a type.
Correct answer is: Handled

Q.8 In Python, what is the base class for all exceptions?

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

Q.9 Which of the following best describes 'try-except' in Python?

A loop structure
A conditional structure
An error handling mechanism
A variable declaration
Explanation - The try-except block in Python is used for handling runtime exceptions.
Correct answer is: An error handling mechanism

Q.10 In Java, which keyword is used to manually throw an exception?

throw
throws
raise
catch
Explanation - The 'throw' keyword is used to explicitly throw an exception in Java.
Correct answer is: throw

Q.11 Which type of exceptions must be declared in the method signature in Java?

Unchecked exceptions
Checked exceptions
Runtime exceptions
Errors
Explanation - Checked exceptions must be declared using the 'throws' keyword.
Correct answer is: Checked exceptions

Q.12 What is the role of the 'throws' keyword in Java?

Handles exceptions
Declares exceptions that a method might throw
Creates exceptions
Suppresses exceptions
Explanation - The 'throws' keyword is used in method signatures to declare exceptions.
Correct answer is: Declares exceptions that a method might throw

Q.13 Which of the following is an unchecked exception in Java?

IOException
SQLException
NullPointerException
FileNotFoundException
Explanation - NullPointerException is a runtime exception, which is unchecked.
Correct answer is: NullPointerException

Q.14 In Python, what happens if the except block doesn’t specify an exception type?

Only syntax errors are caught
All exceptions are caught
No exceptions are caught
Only runtime errors are caught
Explanation - Without specifying an exception type, the except block will catch all exceptions.
Correct answer is: All exceptions are caught

Q.15 Which of the following is NOT a built-in exception in Python?

ValueError
KeyError
TypeError
CompileError
Explanation - Python does not have a 'CompileError' exception.
Correct answer is: CompileError

Q.16 What is the primary purpose of exception handling?

To avoid compilation errors
To handle runtime errors gracefully
To reduce memory usage
To improve speed
Explanation - Exception handling ensures that runtime errors are managed without crashing the program.
Correct answer is: To handle runtime errors gracefully

Q.17 Which of these blocks must always accompany a try block in Java?

catch
finally
catch or finally
throws
Explanation - A try block must be followed by either a catch block or a finally block in Java.
Correct answer is: catch or finally

Q.18 In Python, how do you handle multiple exceptions?

Multiple try blocks
Chained except blocks
Nested functions
if-else
Explanation - Python allows handling multiple exceptions using multiple except clauses.
Correct answer is: Chained except blocks

Q.19 What type of exception is IndexError in Python?

Checked
Unchecked
Compile-time
Custom
Explanation - IndexError occurs at runtime and is considered an unchecked exception.
Correct answer is: Unchecked

Q.20 In C++, which keyword is used to handle exceptions?

except
handle
catch
final
Explanation - C++ uses the try, catch, and throw keywords for exception handling.
Correct answer is: catch

Q.21 What is the difference between Error and Exception in Java?

Errors are recoverable, Exceptions are not
Errors are not recoverable, Exceptions are recoverable
Both are the same
Errors occur only at compile time
Explanation - Errors usually represent serious issues not intended to be caught, while Exceptions are recoverable.
Correct answer is: Errors are not recoverable, Exceptions are recoverable

Q.22 Which Python exception is raised when dividing by zero?

ValueError
ZeroDivisionError
ArithmeticError
TypeError
Explanation - ZeroDivisionError is raised when a division by zero occurs.
Correct answer is: ZeroDivisionError

Q.23 Which exception is thrown when an array index is out of bounds in Java?

NullPointerException
IndexOutOfBoundsException
IllegalArgumentException
IOException
Explanation - Java throws IndexOutOfBoundsException when an invalid array index is accessed.
Correct answer is: IndexOutOfBoundsException

Q.24 Which block in Python is always executed after try-except, whether or not an exception occurs?

else
final
finally
end
Explanation - The finally block executes always for cleanup actions.
Correct answer is: finally

Q.25 What does 'try-with-resources' in Java ensure?

Automatic garbage collection
Automatic resource management
Automatic variable initialization
Automatic debugging
Explanation - The try-with-resources statement ensures automatic closing of resources like streams.
Correct answer is: Automatic resource management