In Java NIO, what is a key advantage of FileChannel over traditional I/O streams?
AIt automatically handles encoding
BIt supports memory mapping and direct buffer access
CIt doesn't require closing
DIt's easier to use than streams
Correct Answer:
B. It supports memory mapping and direct buffer access
EXPLANATION
FileChannel supports memory-mapped files and direct buffer manipulation, enabling high-performance I/O operations not possible with traditional streams.
What happens when you attempt to read from a closed InputStream?
AReturns -1
BThrows IOException
CReturns empty byte array
DSilently ignores the read request
Correct Answer:
B. Throws IOException
EXPLANATION
Attempting to read from a closed stream throws an IOException with message 'Stream closed', preventing accidental data processing from invalid sources.
In a multi-threaded application, which stream class is thread-safe?
AFileInputStream
BBufferedReader
CPrintWriter
DFileOutputStream
Correct Answer:
C. PrintWriter
EXPLANATION
PrintWriter is synchronized, making it thread-safe. Other stream classes are not inherently thread-safe and require external synchronization in multi-threaded contexts.
In a real-time application processing sensor data streams, which buffering approach would minimize latency?
AIncrease buffer size to 64KB
BUse unbuffered I/O or very small buffer sizes with frequent flush()
CUse ObjectInputStream for all data
DImplement custom byte arrays
Correct Answer:
B. Use unbuffered I/O or very small buffer sizes with frequent flush()
EXPLANATION
For real-time applications requiring minimal latency, smaller buffer sizes and frequent flushing ensure data is processed immediately rather than waiting for buffer to fill.
What exception is thrown when trying to deserialize an object that was serialized with a different serialVersionUID?
AClassCastException
BInvalidClassException
CStreamCorruptedException
DNotSerializableException
Correct Answer:
B. InvalidClassException
EXPLANATION
InvalidClassException is thrown when a serialized object's serialVersionUID doesn't match the current class definition, indicating an incompatible class version.