In a multi-threaded application, which stream class is thread-safe?
Answer: C
PrintWriter is synchronized, making it thread-safe. Other stream classes are not inherently thread-safe and require external synchronization in multi-threaded contexts.
Q.542Medium
Which method of PushbackInputStream allows you to return bytes to the stream to be read again?
Answer: B
The unread() method of PushbackInputStream allows you to push bytes back into the stream so they can be read again.
Q.543Hard
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.
Q.544Easy
What happens when you attempt to read from a closed InputStream?
Answer: B
Attempting to read from a closed stream throws an IOException with message 'Stream closed', preventing accidental data processing from invalid sources.
Q.545Hard
In Java NIO, what is a key advantage of FileChannel over traditional I/O streams?
Answer: B
FileChannel supports memory-mapped files and direct buffer manipulation, enabling high-performance I/O operations not possible with traditional streams.
Advertisement
Q.546Hard
Which serialization approach maintains backward compatibility better?
Answer: C
Maintaining a constant serialVersionUID and implementing version-aware readObject() methods allows deserializing objects from different class versions.
Q.547Medium
For processing delimited text files with variable field counts, which class is most suitable?
Answer: B
BufferedReader combined with custom parsing logic (using split or regex) handles variable-length fields better than fixed-format alternatives.
Q.548Medium
What is the primary purpose of transient keyword in Java serialization?
Answer: B
The transient keyword marks fields that should not be serialized, useful for excluding sensitive data or non-serializable objects.
Q.549Medium
In a file compression utility, which stream would you use for reading compressed data?
Answer: B
GZIPInputStream decompresses GZIP-compressed data, allowing transparent reading of compressed files as normal streams.
Q.550Medium
When combining multiple input sources into one logical stream, which class should be used?
Answer: C
SequenceInputStream concatenates multiple input streams, reading from them sequentially as if they were a single stream.
Q.551Hard
For developing a high-performance file copying utility handling files up to 10GB, which approach is optimal?
Answer: C
FileChannel.transferTo() uses OS-level optimizations for file copying, and NIO channels with direct buffers are most efficient for large file transfers.
Q.552Easy
Which class in Java is used to read primitive data types from a binary stream?
Answer: A
DataInputStream is specifically designed to read primitive data types (int, float, double, etc.) from a binary stream using methods like readInt(), readFloat(), etc.
Q.553Easy
What is the output of the following code?
FileReader fr = new FileReader("test.txt");
int data = fr.read();
System.out.println(data);
Answer: B
FileReader.read() returns the ASCII/Unicode value of the character as an integer, not the character itself. It returns -1 when EOF is reached.
Q.554Easy
Which interface must be implemented by a class to make its objects serializable?
Answer: B
The Serializable interface is a marker interface that indicates a class can be serialized. It's found in java.io package and requires no methods to be implemented.
Q.555Easy
What does the flush() method do in output streams?
Answer: B
flush() forces any buffered data to be written to the underlying stream immediately, ensuring data is not lost in the buffer.
Q.556Easy
Which class provides buffering capability to improve I/O performance?
Answer: B
BufferedInputStream wraps another InputStream and buffers input, reducing the number of actual read operations and improving performance significantly.
Q.557Medium
What is the purpose of the mark() and reset() methods in buffered streams?
Answer: A
mark() records the current position in the stream, and reset() returns to that marked position, useful for lookahead operations.
Q.558Medium
Consider a scenario where you need to read a line from a file. Which class would be most efficient?
Answer: B
BufferedReader.readLine() is the most efficient way to read complete lines as it buffers input and provides a dedicated method for line reading.
Q.559Easy
Which stream class is used to write primitive data types in binary format?
Answer: B
DataOutputStream provides methods like writeInt(), writeFloat(), writeDouble() to write primitive types in binary format.
Q.560Medium
In a multi-threaded application, what is a concern when sharing streams between threads?
Answer: B
Streams are not thread-safe. Concurrent access by multiple threads can lead to data corruption. Synchronization or separate streams per thread are needed.