In the try-with-resources statement, what happens to resources automatically?
AThey are flushed but not closed
BThey are closed in reverse order of opening
CThey are closed in the order they were opened
DThey must be manually closed in finally block
Correct Answer:
B. They are closed in reverse order of opening
EXPLANATION
Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.
Which class wraps a byte stream to handle character encoding/decoding?
AInputStreamReader
BBufferedReader
CFileReader
DCharArrayReader
Correct Answer:
A. InputStreamReader
EXPLANATION
InputStreamReader is a bridge between byte streams and character streams. It converts bytes to characters using specified character encoding (UTF-8, ISO-8859-1, etc.).
What happens if you try to read from a closed stream?
AIt returns -1
BIt returns null
CIt throws IOException
DIt silently fails
Correct Answer:
C. It throws IOException
EXPLANATION
Attempting to read from a closed stream throws IOException. The stream should be checked and reopened if needed, or proper resource management (try-with-resources) should be used.
You need to read a file line by line efficiently. Which approach is best?
AFileInputStream with byte-by-byte reading
BBufferedReader with readLine()
CFileReader with manual character buffering
DScanner with default buffer
Correct Answer:
B. BufferedReader with readLine()
EXPLANATION
BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.
What is the purpose of the serialVersionUID field in a serializable class?
ATo identify the version of the JVM
BTo ensure version compatibility during deserialization
CTo encrypt the serialized data
DTo improve serialization speed
Correct Answer:
B. To ensure version compatibility during deserialization
EXPLANATION
serialVersionUID is used to verify that sender and receiver have compatible versions of a Serializable class. If serialVersionUID doesn't match during deserialization, InvalidClassException is thrown.
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
AIOException
BNotSerializableException
CSerializationException
DInvalidObjectException
Correct Answer:
B. NotSerializableException
EXPLANATION
NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.
Which of the following best describes the relationship between InputStream and Reader classes?
AReader extends InputStream
BInputStream extends Reader
CBoth extend a common abstract class
DThey are independent, parallel hierarchies for byte and character streams
Correct Answer:
D. They are independent, parallel hierarchies for byte and character streams
EXPLANATION
InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.
What will be the output of the following code?
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
AThe file should be read-only
BThe file should be overwritten
CThe data should be appended to the existing file
DThe file should be encrypted
Correct Answer:
C. The data should be appended to the existing file
EXPLANATION
The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.