What will be printed for this code?
public class ExceptionOrder {
public static void main(String[] args) {
try {
try {
throw new RuntimeException("Inner");
} catch (Exception e) {
throw new IOException("Outer");
}
} catch (IOException e) {
System.out.println("Caught IOException");
} catch (RuntimeException e) {
System.out.println("Caught RuntimeException");
}
}
}
Answer: C
IOException is a checked exception and must be declared in method signature or caught. The inner catch throws IOException which isn't caught immediately.
Q.102Hard
Which of the following scenarios will NOT trigger a StackOverflowError?
Answer: D
StackOverflowError occurs from stack overflow due to excessive method calls. Local variables contribute to stack but won't cause overflow like recursion.
Q.103Hard
What happens when System.exit() is called inside try block?
Answer: B
System.exit() terminates the JVM immediately. The finally block does not execute as the program terminates before reaching it.
Q.104Hard
Which exception is the parent class of all checked exceptions in Java (excluding RuntimeException)?
Answer: B
Exception class is the parent of all checked exceptions. RuntimeException extends Exception but is unchecked. Error and Throwable are higher in hierarchy.
Q.105Hard
Consider a scenario where multiple catch blocks are written. What is the correct order?
I. catch(FileNotFoundException e)
II. catch(IOException e)
III. catch(Exception e)
Answer: A
Catch blocks should be ordered from most specific to most general exception types. FileNotFoundException is more specific than IOException, which is more specific than Exception.
Advertisement
Q.106Hard
What will this code output?
int result = 0;
try {
result = 010;
} catch(Exception e) {
result = 20;
return result;
} finally {
result = 30;
}
System.out.println(result);
Answer: A
Return statement in catch block prepares the return value (20). Finally block executes but return happens after finally, so 20 is returned even though finally sets result to 30.
Q.107Hard
Which of the following custom exceptions would be inappropriate to extend from RuntimeException in a banking application?
Answer: D
DatabaseConnectionException should be checked (extends Exception) as it's a serious, recoverable error requiring explicit handling. Business logic exceptions can be unchecked.
Q.108Hard
In exception chaining, what is the primary benefit?
Answer: B
Exception chaining preserves the stack trace and cause information: throw new RuntimeException("msg", originalException). This helps in debugging by maintaining the complete exception history.
Q.109Hard
If a finally block contains a return statement, what happens to an exception thrown in the try block?
Answer: A
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.
Q.110Hard
In Java 7+, exception handling introduced 'multi-catch' feature using the pipe (|) operator. What's a limitation of multi-catch blocks?
Answer: B
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.
Q.111Hard
Which scenario would cause a StackOverflowError in Java exception handling?
Answer: B
StackOverflowError occurs when stack memory exhausts due to deep recursion. If a method catches an exception and immediately rethrows it without modification, calling itself, stack frames accumulate until overflow. This is a runtime error, not a checked exception.
Q.112Hard
In Java NIO, which class replaces traditional Stream-based I/O for better performance?
Answer: B
Java NIO (New I/O) uses ByteBuffer and Channels for non-blocking, scalable I/O operations with better performance than traditional streams.
Q.113Hard
What happens if you call close() multiple times on a stream?
Answer: B
Most streams in Java are idempotent regarding close() - calling it multiple times is safe and subsequent calls typically do nothing.
Q.114Hard
Which of the following is true about serialization in Java?
Answer: B
A class must explicitly implement the Serializable interface to be serialized. Static fields are not serialized, and not all classes are serializable by default.
Q.115Hard
In a multi-threaded environment, which stream class provides thread-safe read/write operations without external synchronization?
Answer: A
PrintStream (used by System.out) is internally synchronized for thread-safety, unlike other stream classes.
Q.116Hard
A developer uses PipedInputStream and PipedOutputStream in the same thread. What will happen?
Answer: B
Pipes require separate threads for reading and writing. Using both in the same thread causes deadlock.
Q.117Hard
A program reads a 1 GB binary file and needs to modify specific bytes at random positions. Which class is most suitable?
Answer: C
RandomAccessFile allows seeking to any position in the file without sequential reading, ideal for random access.
Q.118Hard
In Java NIO, what is the primary advantage of FileChannel over traditional streams?
Answer: B
FileChannel enables non-blocking operations and memory-mapping, offering better performance for large files.
Q.119Hard
For a real-time log file monitoring application, which approach is most suitable?
Answer: B
RandomAccessFile allows seeking to the file's end to read only new appended data efficiently.
Q.120Hard
For encrypting data while writing to a file, which approach is most appropriate?
Answer: B
CipherOutputStream allows transparent encryption of data written through it, properly integrating with Java's cryptography framework.