Which of the following is a checked exception in Java?
Answer: B
IOException is a checked exception that must be caught or declared in the method signature. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions.
Q.2Easy
Which exception is thrown when a method receives an illegal or inappropriate argument?
Answer: B
IllegalArgumentException is thrown to indicate that a method has been passed an illegal or inappropriate argument. It is an unchecked exception.
Q.3Easy
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.4Easy
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.5Easy
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.6Easy
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.7Easy
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.8Easy
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.9Easy
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.10Easy
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.11Easy
Which of the following exceptions is a checked exception in Java?
Answer: B
SQLException is a checked exception that must be caught or declared. NullPointerException, ArrayIndexOutOfBoundsException, and ArithmeticException are unchecked exceptions (RuntimeException subclasses).
Q.12Easy
What is the correct syntax for declaring multiple exceptions in a method signature?
Answer: A
Multiple exceptions are declared using comma-separated syntax in the throws clause. Parentheses and pipe operators are not valid syntax for this purpose.
Q.13Easy
In Java exception handling, what is the purpose of the finally block?
Answer: B
The finally block executes unconditionally after try and/or catch blocks complete, making it ideal for resource cleanup. It runs even if try or catch blocks return.
Q.14Easy
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.15Easy
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.16Easy
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.17Easy
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.18Easy
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.19Easy
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.20Easy
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'.