In Java NIO, which class is used for channel-based I/O?
AFileInputStream
BFileChannel
CBufferedInputStream
DDataInputStream
Correct Answer:
B. FileChannel
EXPLANATION
FileChannel provides non-blocking and efficient I/O operations, memory-mapped file access, and file locking capabilities compared to traditional streams.
What is the difference between InputStreamReader and FileReader?
AInputStreamReader accepts any InputStream; FileReader is specifically for files
BFileReader is faster than InputStreamReader
CInputStreamReader supports custom character encoding; FileReader doesn't
DBoth A and C
Correct Answer:
D. Both A and C
EXPLANATION
InputStreamReader is a bridge from byte streams to character streams and accepts any InputStream with optional charset. FileReader is specifically for file input with default charset.
In a multi-threaded application, what is a concern when sharing streams between threads?
AMemory leaks
BData corruption due to concurrent access
CIncreased CPU usage
DAutomatic buffering issues
Correct Answer:
B. Data corruption due to concurrent access
EXPLANATION
Streams are not thread-safe. Concurrent access by multiple threads can lead to data corruption. Synchronization or separate streams per thread are needed.
Which class provides buffering capability to improve I/O performance?
AFileInputStream
BBufferedInputStream
CByteArrayInputStream
DPipedInputStream
Correct Answer:
B. BufferedInputStream
EXPLANATION
BufferedInputStream wraps another InputStream and buffers input, reducing the number of actual read operations and improving performance significantly.