In Java NIO, which class replaces traditional Stream-based I/O for better performance?
Answer: B
Java NIO (New I/O) uses ByteBuffer and Channels for non-blocking, scalable I/O operations with better performance than traditional streams.
Q.2Hard
What happens if you call close() multiple times on a stream?
Answer: B
Most streams in Java are idempotent regarding close() - calling it multiple times is safe and subsequent calls typically do nothing.
Q.3Hard
Which of the following is true about serialization in Java?
Answer: B
A class must explicitly implement the Serializable interface to be serialized. Static fields are not serialized, and not all classes are serializable by default.
Q.4Hard
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.5Hard
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.6Hard
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.7Hard
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.8Hard
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.9Hard
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.10Hard
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.11Hard
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.12Hard
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.13Hard
Given the code: PrintWriter pw = new PrintWriter(new FileWriter("file.txt")); What issue might occur?
Answer: B
PrintWriter uses buffering by default. If flush() or close() is not called, buffered data may not be written to disk.
Q.14Hard
Which approach would you use to read and write objects to a file efficiently in a production application?
Answer: B
ObjectInputStream/ObjectOutputStream with serialVersionUID ensures compatibility, versioning, and is the standard approach for object persistence in Java.
Q.15Hard
In a log aggregation system, what is the primary advantage of using RandomAccessFile over sequential streams?
Answer: B
RandomAccessFile supports seek() operation to jump to specific file positions, useful for reading log tail or accessing specific records directly.
Q.16Hard
What is the main advantage of using RandomAccessFile over sequential streams?
Answer: B
RandomAccessFile allows you to read/write at any position in a file using seek() method. Sequential streams can only read/write sequentially from current position.
Q.17Hard
When deserializing an object, which methods are called in order?
Answer: B
During deserialization, the constructor is NOT called. readObject() restores the object state directly from the serialized data. This is why transient fields need special handling.
Q.18Hard
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
Answer: B
BufferedInputStream provides automatic buffering (8KB default), and DataInputStream allows reading typed data. Together they enable efficient reading of large binary files without loading everything into memory.
Q.19Hard
What is the primary advantage of PushbackInputStream?
Answer: B
PushbackInputStream allows you to 'unread' bytes using the unread() method. This is useful for parsers that need to look ahead without consuming the data.
Q.20Hard
In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
Answer: B
FileOutputStream is NOT thread-safe. When multiple threads write simultaneously, race conditions can cause data corruption or interleaved writes. Solutions include: synchronizing access using synchronized blocks, using thread-safe wrapper classes, or employing a single-threaded writer pattern with a queue. PrintWriter with autoFlush provides some buffering protection.