Which of the following correctly demonstrates chaining multiple filters in Java I/O?
Answer: A
Filter streams are chained from inside-out. FileReader is wrapped with BufferedReader for better performance.
Q.22Medium
A serialized object contains a field marked as 'transient'. What happens to this field when the object is deserialized?
Answer: B
Transient fields are excluded from serialization and are initialized to default values upon deserialization.
Q.23Hard
In a multi-threaded environment, which stream class provides thread-safe read/write operations without external synchronization?
Answer: A
PrintStream (used by System.out) is internally synchronized for thread-safety, unlike other stream classes.
Q.24Easy
What will be the output of reading from a ByteArrayInputStream initialized with bytes [65, 66, 67] after calling read() three times?
Answer: A
read() returns integer values of bytes. [65, 66, 67] are ASCII values for 'A', 'B', 'C'. Multiple read() calls return these values sequentially.
Q.25Hard
A developer uses PipedInputStream and PipedOutputStream in the same thread. What will happen?
Answer: B
Pipes require separate threads for reading and writing. Using both in the same thread causes deadlock.
Advertisement
Q.26Medium
Which statement about ObjectOutputStream.reset() is correct?
Answer: B
reset() clears ObjectOutputStream's internal cache of serialized objects, allowing duplicate objects to be serialized again.
Q.27Medium
When implementing Externalizable interface, which method is mandatory?
Answer: C
Externalizable requires implementation of writeExternal() and readExternal() for complete control over serialization.
Q.28Easy
A RandomAccessFile is opened with mode 'r'. Which operation will throw an exception?
Answer: C
Mode 'r' allows read-only access. Write operations will throw IOException.
Q.29Medium
In a CSV file processing application, why is BufferedReader preferred over FileReader for reading large files?
Answer: B
BufferedReader buffers input, reducing system calls and I/O overhead, improving performance significantly.
Q.30Medium
What is the serialVersionUID used for in Java serialization?
Answer: B
serialVersionUID identifies class versions. Mismatched versions cause InvalidClassException during deserialization.
Q.31Hard
A program reads a 1 GB binary file and needs to modify specific bytes at random positions. Which class is most suitable?
Answer: C
RandomAccessFile allows seeking to any position in the file without sequential reading, ideal for random access.
Q.32Medium
What happens when you write to a closed PrintWriter?
Answer: C
PrintWriter suppresses IOException and continues silently, unlike other streams.
Q.33Hard
In Java NIO, what is the primary advantage of FileChannel over traditional streams?
Answer: B
FileChannel enables non-blocking operations and memory-mapping, offering better performance for large files.
Q.34Medium
Which scenario would require using a SequenceInputStream?
Answer: A
SequenceInputStream concatenates multiple input streams, allowing them to be read as one continuous stream.
Q.35Medium
A file contains 1000 lines. Using BufferedReader.readLine() repeatedly reads all lines. What is the time complexity?
Answer: C
Reading each line requires processing each character once, resulting in O(n) complexity where n is total characters.
Q.36Hard
For a real-time log file monitoring application, which approach is most suitable?
Answer: B
RandomAccessFile allows seeking to the file's end to read only new appended data efficiently.
Q.37Easy
Which class in Java I/O streams is used to read primitive data types from a binary file?
Answer: A
DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a binary file.
Q.38Easy
What is the purpose of the flush() method in PrintWriter?
Answer: B
The flush() method clears the internal buffer and forces any buffered output to be written to the underlying stream without closing it.
Q.39Easy
Which of the following is a character-based stream in Java?
Answer: C
FileReader is a character-based stream that reads characters from a file. InputStream and its subclasses are byte-based streams.
Q.40Medium
In a real-time application processing sensor data streams, which buffering approach would minimize latency?
Answer: B
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.