What is the difference between 'throw' and 'throws' keywords in Java?
Answer: B
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.
Q.42Medium
Consider this code snippet. What will happen?
try {
int x = 05;
} catch (NullPointerException e) {
System.out.println("Caught NPE");
}
Answer: B
05 throws ArithmeticException, not NullPointerException. The catch block only handles NPE, so ArithmeticException propagates to the caller.
Q.43Medium
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");
}
}
}
Answer: B
ArrayIndexOutOfBoundsException is caught by Exception catch block, printing 'Exception caught'. Finally block always executes regardless, printing 'Finally block'.
Q.44Easy
Which exception is thrown when a method argument is null but shouldn't be?
Answer: B
NullPointerException is thrown when attempting to use an object reference that hasn't been instantiated. Java doesn't have NullArgumentException.
Q.45Medium
What happens if you use 'return' statement inside a finally block?
Answer: B
A return statement in finally will override and suppress any return value or exception from try/catch blocks. This is considered a bad practice.
Advertisement
Q.46Hard
Which of the following is NOT a characteristic of Exception class in Java?
Answer: B
Exception class has both checked subclasses (like IOException) and unchecked subclasses (like RuntimeException). Not all exceptions extending Exception are checked.
Q.47Medium
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");
}
}
}
Answer: B
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.
Q.48Hard
Which of the following cannot throw a checked exception without being declared in throws clause?
Answer: D
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.
Q.49Medium
What is the output of exception chaining in Java?
Answer: B
Exception chaining wraps a caught exception within another exception using initCause() or constructor, preserving the original exception for debugging and stack trace analysis.
Q.50Medium
Which method is used to get the exception that caused the current exception in Java?
Answer: B
The getCause() method returns the Throwable that caused the current exception, enabling exception chaining analysis and debugging.
Q.51Easy
What happens when an unchecked exception is not caught?
Answer: B
Unchecked exceptions (RuntimeException subclasses) don't require explicit handling. If not caught, they propagate up the call stack, eventually terminating the thread if not caught anywhere.
Q.52Easy
Which exception is thrown when attempting to access an index outside the bounds of an array?
Answer: B
ArrayIndexOutOfBoundsException is thrown when attempting to access array elements with invalid indices. It's an unchecked exception extending RuntimeException.
Q.53Hard
What is the execution order in try-catch-finally for nested exception handling?
Answer: B
Nested exception handling follows inner-to-outer order. Inner try-catch-finally completes fully, then control passes to outer blocks. Finally blocks execute in their respective scope order.
Q.54Easy
Consider a method signature: public void readFile() throws IOException. Which statement is correct?
Answer: B
throws declaration indicates the method may throw IOException. The caller is responsible for handling it with try-catch or declaring throws. The method doesn't always throw it.
Q.55Hard
What is suppressed exception in Java (introduced in Java 7)?
Answer: B
In try-with-resources, if an exception occurs in try and another in resource close(), the second is added as suppressed exception to the first using addSuppressed(), preserving all exception information.
Q.56Easy
Which of the following statements about try-catch-finally blocks is correct?
Answer: A
The finally block always executes regardless of whether a return, break, continue, or exception occurs in try or catch blocks, unless System.exit() is called.
Q.57Easy
What is the output of the following code?
public class ExceptionTest {
public static void main(String[] args) {
try {
int x = 010;
} catch (ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
}
}
Answer: A
ArithmeticException is caught by the catch block printing 'Caught', then finally block executes printing 'Finally'.
Q.58Easy
What will be the output?
public class Test {
public static void main(String[] args) {
try {
throw new Exception("Test");
} catch (Exception e) {
System.out.println("1");
} catch (Exception e) {
System.out.println("2");
}
}
}
Answer: C
Multiple catch blocks with the same exception type cause compilation error. Java doesn't allow duplicate catch blocks for identical exception types.
Q.59Medium
Which exception is thrown when a thread tries to acquire a monitor that is already locked?
Answer: B
IllegalMonitorStateException is thrown when an invalid monitor state operation occurs, such as calling notify() on an object not owned by the current thread.
Q.60Medium
Consider the following code. What will be printed?
public class FinallyTest {
public static int getValue() {
try {
return 10;
} finally {
System.out.println("Finally");
}
}
public static void main(String[] args) {
System.out.println(getValue());
}
}
Answer: A
finally block executes before the return statement completes. System.out.println in finally executes first, then the return value 10 is printed.