Which class would you use to read and write at arbitrary positions in a file?
Answer: C
RandomAccessFile allows reading and writing at arbitrary positions using seek() method, useful for direct access to file contents.
Q.42Medium
When processing a large XML file, which approach is most memory-efficient?
Answer: C
SAX parsers and streaming XML parsers process XML sequentially without loading the entire document, making them ideal for large files.
Q.43Medium
What is the default charset used by InputStreamReader if none is specified?
Answer: C
If no charset is explicitly provided, InputStreamReader uses the platform's default charset, which varies by system configuration.
Q.44Medium
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.45Medium
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.
Advertisement
Q.46Hard
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.47Easy
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.48Hard
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.
Q.49Hard
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.50Medium
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.51Medium
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.52Medium
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.53Medium
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.54Hard
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.55Easy
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.56Easy
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.57Easy
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.58Easy
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.59Easy
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.60Medium
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.