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");
}
}
}
ACaught
BArrayIndexOutOfBoundsException
CCompilation error
DNo output
Correct Answer:
A. Caught
EXPLANATION
ArrayIndexOutOfBoundsException is caught by the generic Exception catch block, printing 'Caught'.
Analyze the nested try-catch:
try {
try {
int x = 5/0;
} catch(NullPointerException e) {
System.out.println("Inner");
}
} catch(ArithmeticException e) {
System.out.println("Outer");
}
AInner
BOuter
CInner Outer
DNeither prints
Correct Answer:
B. Outer
EXPLANATION
ArithmeticException is thrown by 5/0. Inner catch only catches NullPointerException, so it propagates to outer catch which handles ArithmeticException. Output: Outer
What will happen if you don't catch a checked exception?
ACompilation error
BRuntime error
CIt will be automatically handled
DProgram will run successfully
Correct Answer:
A. Compilation error
EXPLANATION
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.
BThrowable → Exception → RuntimeException and Throwable → Error
CException → Throwable → Error → RuntimeException
DRuntimeException → Exception → Throwable → Error
Correct Answer:
B. Throwable → Exception → RuntimeException and Throwable → Error
EXPLANATION
Throwable is the root. It has two subclasses: Exception and Error. Exception has RuntimeException as a subclass. Checked exceptions extend Exception directly.
Analyze this code:
public void test() throws IOException {
// method body
}
What does 'throws' indicate?
AThe method will definitely throw IOException
BThe method might throw IOException and caller must handle it
CIOException is caught inside the method
DIOException is prevented from being thrown
Correct Answer:
B. The method might throw IOException and caller must handle it
EXPLANATION
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.
Which exception is thrown when a string cannot be converted to a number?
ANumberFormatException
BConversionException
CParseException
DStringConversionException
Correct Answer:
A. NumberFormatException
EXPLANATION
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.
What is the output?
try {
int x = 5/0;
} catch(Exception e) {
System.out.println("Caught");
} catch(ArithmeticException ae) {
System.out.println("Arithmetic");
}
ACaught
BArithmetic
CCompilation Error
DCaught Arithmetic
Correct Answer:
C. Compilation Error
EXPLANATION
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.
CResources are automatically closed in reverse order of opening
DIt cannot be used with multiple resources
Correct Answer:
C. Resources are automatically closed in reverse order of opening
EXPLANATION
Try-with-resources (Java 7+) automatically closes resources implementing AutoCloseable. Resources are closed in reverse order. Multiple resources can be declared with semicolon separation.