BOne exception wraps another exception preserving the original cause
CAll exceptions are converted to RuntimeException
DThe first exception is ignored
Correct Answer:
B. One exception wraps another exception preserving the original cause
EXPLANATION
Exception chaining wraps a caught exception within another exception using initCause() or constructor, preserving the original exception for debugging and stack trace analysis.
Which of the following cannot throw a checked exception without being declared in throws clause?
AConstructor
BInstance initializer block
CStatic initializer block
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
Checked exceptions in constructors, instance initializers, and static initializers must be handled with try-catch or declared in throws (constructors only). This is enforced by Java compiler.
What will be printed?
public class Test {
public static void main(String[] args) {
try {
System.out.println("In try");
return;
} catch (Exception e) {
System.out.println("In catch");
} finally {
System.out.println("In finally");
}
}
}
AIn try\nIn catch\nIn finally
BIn try\nIn finally
CIn try only
DCompilation error
Correct Answer:
B. In try\nIn finally
EXPLANATION
Try block executes printing 'In try', then return is encountered. Finally block still executes printing 'In finally' before method returns. Catch is skipped as no exception occurs.
Which of the following is NOT a characteristic of Exception class in Java?
AIt extends Throwable
BIt is always checked exception
CIts subclasses may be checked or unchecked
DIt can be instantiated directly
Correct Answer:
B. It is always checked exception
EXPLANATION
Exception class has both checked subclasses (like IOException) and unchecked subclasses (like RuntimeException). Not all exceptions extending Exception are checked.
What is the output of this code?
public class ExceptionDemo {
public static void main(String[] args) {
try {
int arr[] = new int[2];
arr[5] = 10;
} catch (Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block");
}
}
}
AException caught
BException caught\nFinally block
CFinally block
DNo output
Correct Answer:
B. Exception caught\nFinally block
EXPLANATION
ArrayIndexOutOfBoundsException is caught by Exception catch block, printing 'Exception caught'. Finally block always executes regardless, printing 'Finally block'.
What is the difference between 'throw' and 'throws' keywords in Java?
Athrow is used to declare exceptions, throws is used to throw them
Bthrow creates an exception object and throws it, throws declares that a method may throw exceptions
CThey are synonyms and can be used interchangeably
Dthrows is used only with try-catch blocks
Correct Answer:
B. throw creates an exception object and throws it, throws declares that a method may throw exceptions
EXPLANATION
throw explicitly throws an exception object; throws declares in method signature that exceptions may be thrown. Example: throw new IOException() vs public void method() throws IOException.