Can you have a try block without a catch block in Java?
Answer: B
A try block must be followed by either a catch block or a finally block (or both). A try block alone is not valid syntax.
Q.402Medium
What will be the output?
try {
throw new Exception("Test");
} catch(Exception e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
Answer: A
The exception is caught and prints "Caught". The finally block always executes after try-catch, printing "Finally". Output: Caught Finally
Q.403Easy
Which of the following is NOT a subclass of Throwable in Java?
Answer: D
Runnable is a functional interface, not related to exception hierarchy. Error and Exception are direct subclasses of Throwable. RuntimeException is a subclass of Exception.
Q.404Medium
What is the output of multiple catch blocks?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Index");
} catch(Exception e) {
System.out.println("Exception");
}
Answer: A
ArrayIndexOutOfBoundsException is thrown and caught by the first catch block. The second catch block is not executed. Only the most specific matching catch block executes.
Q.405Easy
Which exception is thrown when trying to access a method of a null object?
Answer: B
NullPointerException is thrown when attempting to call a method or access a property on a null object reference in Java.
Advertisement
Q.406Hard
Analyze the code:
try {
return 5;
} finally {
return 10;
}
What will be returned?
Answer: B
The finally block always executes, and if it contains a return statement, it overrides the return value from try or catch block. So 10 is returned.
Q.407Medium
Which of the following exceptions is a checked exception?
Answer: B
FileNotFoundException is a checked exception (extends IOException). Others are unchecked - ClassCastException and NullPointerException are RuntimeExceptions, StackOverflowError is an Error.
Q.408Hard
What happens if an exception is thrown in the finally block?
Answer: C
If an exception occurs in the finally block, it will replace any exception thrown in the try or catch block, and the new exception will be propagated.
Q.409Medium
Which statement about try-with-resources is TRUE?
Answer: C
Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.
Q.410Medium
What is the output?
try {
int x = 05;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
Answer: C
This is a compilation error. Catch blocks must be ordered from most specific to most general. ArithmeticException is more specific than Exception, so it should come first.
Q.411Easy
Which exception is thrown when a string cannot be converted to a number?
Answer: A
NumberFormatException is thrown when attempting to convert a string to a numeric type but the string does not have an appropriate format. It's an unchecked exception.
Q.412Medium
Analyze this code:
public void test() throws IOException {
// method body
}
What does 'throws' indicate?
Answer: B
The 'throws' keyword declares that a method might throw the specified checked exception. The responsibility of handling is passed to the caller of the method.
Q.413Medium
Which exception hierarchy is correct in Java?
Answer: B
Throwable is the root. It has two subclasses: Exception and Error. Exception has RuntimeException as a subclass. Checked exceptions extend Exception directly.
Q.414Medium
What will happen if you don't catch a checked exception?
Answer: A
Checked exceptions must be either caught in a try-catch block or declared in the method signature with 'throws'. Failure to do so results in a compilation error.
Q.415Hard
Analyze the nested try-catch:
try {
try {
int x = 05;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
Answer: B
ArithmeticException is thrown by 05. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer
Q.416Easy
What is the output of this code?
public class Test {
public static void main(String[] args) {
try {
int[] arr = {1, 2};
System.out.println(arr[5]);
} catch (Exception e) {
System.out.println("Caught");
}
}
}
Answer: A
ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.
Q.417Easy
In Java exception handling, what is the order of execution in try-finally-catch block?
Answer: A
The correct order is try block executes first, then catch block (if exception occurs), and finally block always executes at the end.
Q.418Easy
Which exception is thrown when a numeric string cannot be converted to a number?
Answer: A
NumberFormatException is thrown by Integer.parseInt(), Double.parseDouble() when string format is invalid.
Q.419Easy
What will be the output?
public class Demo {
public static void main(String[] args) {
try {
System.out.println("A");
throw new Exception("Test");
System.out.println("B");
} catch (Exception e) {
System.out.println("C");
}
}
}
Answer: B
After 'A' is printed, exception is thrown. Statement 'B' is never executed. Catch block prints 'C'.
Q.420Medium
Which statement about multiple catch blocks is TRUE?
Answer: B
Only one catch block executes for a thrown exception. The first matching catch block is executed.