WorksheetsQuiz 1: Exception Handling
Total questions: 20
Worksheet time: 10mins
What is an Exception in Java?
An error that occurs during runtime
An error that occurs during compilation
A warning issued by the compiler
A type of loop
Which keyword is used to handle exceptions in Java?
throws
throw
try
catch
What is the purpose of "try" block in exception handling?
It catches exceptions and handles them
It specifies the exception type
It contains the code that might throw an exception
It specifies the exception message
In Java, can a method declare multiple exceptions using the "throws" keyword?
Only if the method is marked as "static"
Yes, a method can declare multiple exception separated by commas
Only if the exceptions are marked as "final"
Only if the method is marked as "final"
What is the purpose of "catch" block in exception handling?
It specifies the exception message
It specifies the exception type
It catches the exceptions and handles them
It contains the code that might throw an exception
In Java, can a "catch" block catch multiple exceptions?
No, a "catch" block can only catch one exception
Only if the "catch" block is marked as "static"
Only if the exceptions are marked as "final"
Yes, a "catch" block can catch multiple exceptions using the pipe (|) symbol
What is the purpose of the "throw" keyword in Java exception handling?
To explicitly throw an exception
To catch exceptions
To specify exception types
To declare a method as "throws"
In Java, can a method throw any checked exception without declaring it using "throws"?
Only if the exception is marked as "static"
Only if the method is marked as "final"
No, a method can declare checked exceptions using the "throws" keyword
Yes, a method can throw any checked exception without declaration
What is the purpose of the "throws" keyword in Java method declaration?
To indicate that the method will throw an unchecked exception
To specify the exception message
To indicate that the method may throw a checked exception
To specify the exception type
What is the result of the following code snippet?
try {
int[] arr = new int[5];
int value = arr[10];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught!");
}
Compilation error
Exception caught!
Runtime exception
Bounds exception
In Java, which type of exceptions are checked at compile time?
Custom exceptions
Runtime exceptions
Checked exceptions
Unchecked exceptions
What is the result of the following code snippet?
try {
int num=10/0;
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception!");
} catch ( Exception e) {
System.out.println("Exception!");
}
Exception!
No output
Compilation error
Arithmetic Exception!
In Java, which type of exceptions are unchecked at compile-time?
Unchecked exceptions
Checked exceptions
Runtime exceptions
Custom exceptions
What is the result of the following code snippet?
try {
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("NullPointerException!");
} catch (Exception e) {
System.out.println("Exception!");
}
Compilation error
No output
Exception!
NullPointerException!
In Java, can a "finally" block be used without a "try-catch" block?
Yes, a "finally" block can be used independently
Only if the "finally" block is marked as "static"
No, a "finally" block must be associated with a "try-catch" block
Only if the "finally" block is marked as "final"
What is the purpose of the "finally" block in java exception handling?
To throw exceptions explicitly
To specify the code that must be executed regardless of whether an exception is thrown or caught
To specify the exception type
When an exception does not rise
In Java, can a "finally" block be used without a "catch" block?
Only if the "finally" block is marked as "final"
Only if the "finally" block is marked as "static"
Yes, a "finally" block can be used independently
No, a "finally" block must be associated with a "catch" block
What is the result of the following code snippet?
try {
int[] arr = new int[5];
int value = arr[2];
} catch (NullPointerException e) {
System.out.println("NullPointerException!");
} finally {
System.out.println("Finally Block!");
}
No output
NullPointerException!
Compilation error
Finally Block!
In Java, can a "catch" block catch exceptions of a superclass type?
Yes, a "catch" block can catch exceptions of a superclass type
No, a "catch" block can only catch exceptions of the exact type
Only if the superclass is marked as "final"
Only if the subclass is marked as "static"
What is the result of the following code snippet?
try {
int result = 10/0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
} finally {
System.out.println("Finally Block!");
}
No output
Finally Block!
ArithmeticException!
ArithmeticException!
Finally Block!
