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.122Easy
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.123Easy
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.124Easy
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.125Easy
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.
Advertisement
Q.126Easy
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.127Easy
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.128Easy
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.129Easy
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.130Easy
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.131Easy
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.132Easy
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.133Easy
Which method is used to print the stack trace of an exception?
Answer: D
printStackTrace() prints to System.err, while getStackTrace() returns an array of StackTraceElement objects. Both exist and serve different purposes.
Q.134Easy
Which of the following is NOT a subclass of Throwable?
Answer: D
Throwable is the superclass. Exception and Error are direct subclasses, and RuntimeException is a subclass of Exception.
Q.135Easy
In Java exception handling, which of the following statements is true about the finally block?
Answer: B
The finally block in Java always executes whether an exception is caught or not, except when System.exit() is called or JVM terminates abnormally.
Q.136Easy
What is the output of the following code?
int x = 10;
try {
x = x / 0;
} catch (ArithmeticException e) {
x = 20;
} finally {
x = 30;
}
System.out.println(x);
Answer: C
The finally block always executes last and sets x to 30. The output will be 30 because finally block assignments override catch block assignments.
Q.137Easy
Which exception is thrown when accessing an array element with an invalid index?
Answer: B
ArrayIndexOutOfBoundsException is thrown when trying to access an array element with an index that is out of the valid range (0 to length-1).
Q.138Easy
What is the correct syntax for declaring a method that throws a checked exception?
Answer: A
The 'throws' keyword is used in method declaration to indicate that the method might throw checked exceptions. Syntax: method() throws ExceptionType {}
Q.139Easy
What will happen when the following code is executed?
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught");
}
Answer: C
Accessing arr[5] when array has only 3 elements throws ArrayIndexOutOfBoundsException which is caught and 'Exception caught' is printed.
Q.140Easy
What is the output of the following code?
try {
int x = 010;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
Answer: A
When ArithmeticException occurs, it's caught by the catch block printing 'Caught'. The finally block always executes, printing 'Finally'. Then normal program flow continues, printing 'After'.