In Java 8+, when using try-with-resources with multiple AutoCloseable resources, in what order are they closed?
Answer: B
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.
Q.242Medium
Which of the following exceptions would NOT be caught by catching Exception class in Java?
Answer: C
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.
Q.243Medium
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?
Answer: B
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.
Q.244Medium
What does the getSuppressed() method of Throwable class return in context of try-with-resources?
Answer: B
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.
Q.245Medium
In Java, when an exception is thrown in a try block and caught in a catch block, if the catch block also throws an exception, what happens to the original exception?
Answer: C
When exception chaining is explicitly used with initCause() or constructor parameters, the original exception becomes the cause of the new exception. Without explicit chaining, the original exception is lost. Exception chaining is not automatic in Java.
Advertisement
Q.246Medium
What is the default buffer size of BufferedReader in Java?
Answer: C
BufferedReader has a default buffer size of 8192 characters, but commonly 2048 bytes is used. The exact implementation may vary, but 8192 is the default character buffer.
Q.247Medium
What happens when you try to read from a closed stream in Java?
Answer: B
Attempting to read from a closed stream throws an IOException. The stream must be open for read operations.
Q.248Medium
Which of the following is a filtered stream?
Answer: B
BufferedOutputStream is a filtered stream that adds buffering capability to an underlying output stream. Filtered streams wrap other streams to add functionality.
Q.249Medium
Which class is used to read primitive data types from a stream?
Answer: B
DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a stream.
Q.250Medium
Which of the following creates a bridge between character and byte streams?
Answer: B
InputStreamReader converts a byte stream (InputStream) into a character stream (Reader), acting as a bridge between the two.
Q.251Medium
What is the difference between FileInputStream and BufferedInputStream?
Answer: B
FileInputStream reads directly from a file byte by byte, while BufferedInputStream wraps another stream and adds buffering to improve performance by reducing I/O operations.
Q.252Medium
Which class allows reading and writing objects to a stream?
Answer: B
ObjectInputStream and ObjectOutputStream are used for serialization and deserialization of Java objects. Objects must implement Serializable interface.
Q.253Medium
What is the mark() method used for in BufferedReader?
Answer: B
The mark() method marks the current position in the stream so it can be returned to later using reset(). Requires markSupported() to return true.
Q.254Medium
Which of the following statements will correctly read a file line by line?
Answer: B
BufferedReader's readLine() method is the standard way to read lines. FileInputStream doesn't have readLine(), FileReader doesn't have readLine() either.
Q.255Medium
What is the purpose of the skip() method in InputStream?
Answer: B
The skip(long n) method skips n bytes in the input stream and returns the number of bytes actually skipped.
Q.256Medium
A Java program needs to read a file containing 10 million integers. Which approach would be most memory-efficient?
Answer: C
DataInputStream with buffering allows sequential reading without loading the entire file into memory, making it ideal for large files.
Q.257Medium
A serialized object contains a field marked as 'transient'. What happens to this field when the object is deserialized?
Answer: B
Transient fields are excluded from serialization and are initialized to default values upon deserialization.
Q.258Medium
Which statement about ObjectOutputStream.reset() is correct?
Answer: B
reset() clears ObjectOutputStream's internal cache of serialized objects, allowing duplicate objects to be serialized again.
Q.259Medium
When implementing Externalizable interface, which method is mandatory?
Answer: C
Externalizable requires implementation of writeExternal() and readExternal() for complete control over serialization.
Q.260Medium
In a CSV file processing application, why is BufferedReader preferred over FileReader for reading large files?
Answer: B
BufferedReader buffers input, reducing system calls and I/O overhead, improving performance significantly.