Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 461–470 of 958
Topics in Java Programming
Q.461 Medium Exception Handling
What does the getSuppressed() method of Throwable class return in context of try-with-resources?
A The primary exception thrown in try block
B Array of exceptions suppressed while closing resources
C The exception thrown in finally block
D Exception from outermost try-catch block
Correct Answer:  B. Array of exceptions suppressed while closing resources
EXPLANATION

When try-with-resources encounters an exception while closing a resource, it's added to the suppressed exceptions list. If an exception was already thrown in try, the close exception is suppressed rather than replacing it. getSuppressed() returns array of these suppressed exceptions.

Test
In Java 7+, exception handling introduced 'multi-catch' feature using the pipe (|) operator. What's a limitation of multi-catch blocks?
A Cannot have multiple exception types
B Exception types must be unrelated (not in same inheritance hierarchy)
C Can only catch maximum of 2 exception types
D Multi-catch syntax is deprecated in Java 17+
Correct Answer:  B. Exception types must be unrelated (not in same inheritance hierarchy)
EXPLANATION

In multi-catch blocks like 'catch(IOException | SQLException e)', the exception types must not have an inheritance relationship. You cannot do 'catch(Exception | IOException e)' because IOException is a subclass of Exception.

Test
What is the primary difference between throw and throws in Java exception handling?
A throw is used in catch block, throws is used in method declaration
B throw explicitly raises an exception, throws declares that a method may throw exceptions
C throw handles exceptions, throws creates exceptions
D Both are identical; throws is just an older syntax
Correct Answer:  B. throw explicitly raises an exception, throws declares that a method may throw exceptions
EXPLANATION

'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}

Test
Q.464 Medium Exception Handling
Consider a scenario where you have nested try-catch blocks. If both inner and outer catch blocks match the thrown exception type, which one executes?
A Both execute in sequence
B Only the inner catch block executes
C Only the outer catch block executes
D The JVM decides based on exception priority
Correct Answer:  B. Only the inner catch block executes
EXPLANATION

The innermost matching catch block executes first. If it doesn't rethrow the exception, the outer catch block won't execute. The exception is considered handled after the first matching catch block.

Test
If a finally block contains a return statement, what happens to an exception thrown in the try block?
A The exception is suppressed and the finally's return value is used
B The exception is propagated after finally executes
C The exception replaces the finally return value
D A compilation error occurs
Correct Answer:  A. The exception is suppressed and the finally's return value is used
EXPLANATION

If finally has a return statement, it suppresses any exception from try/catch blocks. The return value from finally overrides exception propagation. This is generally considered bad practice as it masks exceptions.

Test
What is the output of the following code?
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Caught");
} finally {
System.out.println("Finally");
}
System.out.println("After");
A Caught\nFinally\nAfter
B Caught\nFinally
C Finally\nAfter
D ArithmeticException is thrown, program terminates
Correct Answer:  A. Caught\nFinally\nAfter
EXPLANATION

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'.

Test
Q.467 Medium Exception Handling
Which of the following exceptions would NOT be caught by catching Exception class in Java?
A IOException
B NullPointerException
C Error
D ArithmeticException
Correct Answer:  C. Error
EXPLANATION

Error is not a subclass of Exception. Both Error and Exception extend Throwable. Errors like OutOfMemoryError, StackOverflowError cannot be caught by Exception handlers. They must be caught separately if needed.

Test
Q.468 Medium Exception Handling
In Java 8+, when using try-with-resources with multiple AutoCloseable resources, in what order are they closed?
A In the order they were declared (first declared, first closed)
B In reverse order of declaration (last declared, first closed)
C In random order determined by JVM
D Simultaneously in parallel threads
Correct Answer:  B. In reverse order of declaration (last declared, first closed)
EXPLANATION

Try-with-resources closes resources in LIFO (Last In, First Out) order. If you declare Resource1, then Resource2, Resource2 closes first, then Resource1. This ensures dependencies are respected.

Test
In exception chaining, what is the primary benefit?
A It reduces code size
B It preserves the original exception context while throwing a new exception
C It eliminates the need for catch blocks
D It automatically fixes errors
Correct Answer:  B. It preserves the original exception context while throwing a new exception
EXPLANATION

Exception chaining preserves the stack trace and cause information: throw new RuntimeException("msg", originalException). This helps in debugging by maintaining the complete exception history.

Test
Which of the following custom exceptions would be inappropriate to extend from RuntimeException in a banking application?
A InvalidAccountException
B InsufficientFundsException
C NegativeAmountException
D DatabaseConnectionException
Correct Answer:  D. DatabaseConnectionException
EXPLANATION

DatabaseConnectionException should be checked (extends Exception) as it's a serious, recoverable error requiring explicit handling. Business logic exceptions can be unchecked.

Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips