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.22Medium
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.23Medium
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.24Medium
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.25Medium
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.
Advertisement
Q.26Medium
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.27Medium
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.28Medium
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.29Medium
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.30Medium
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.31Medium
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.
Q.32Medium
What is the difference between InputStreamReader and FileReader?
Answer: D
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.
Q.33Medium
Consider processing a 1GB file. Which approach would be most memory-efficient?
Answer: C
Processing large files in chunks using BufferedInputStream with an appropriate buffer size (like 8KB-64KB) balances memory usage and I/O efficiency.
Q.34Medium
What does the available() method in InputStream return?
Answer: A
available() returns an estimate of bytes that can be read without blocking. It doesn't return total file size.
Q.35Medium
In Java NIO, which class is used for channel-based I/O?
Answer: B
FileChannel provides non-blocking and efficient I/O operations, memory-mapped file access, and file locking capabilities compared to traditional streams.
Q.36Medium
What is the correct way to ensure resources are properly closed in Java 7+?
Answer: B
Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, ensuring proper cleanup even if exceptions occur.
Q.37Medium
What happens when you create a FileOutputStream with an existing file?
Answer: C
FileOutputStream(String filename) by default overwrites existing file. Use FileOutputStream(File, boolean append) with true to append.
Q.38Medium
What is the default buffer size used by BufferedInputStream?
Answer: C
The default buffer size for BufferedInputStream is 8192 bytes (8 KB). This can be overridden by using the constructor BufferedInputStream(InputStream, int size).
Q.39Medium
Which class is used to read primitive data types and strings from a binary stream?
Answer: B
DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.
Q.40Medium
Which of the following best describes the relationship between InputStream and Reader classes?
Answer: D
InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.