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.62Easy
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.63Medium
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.64Medium
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.65Medium
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.
Advertisement
Q.66Medium
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.67Medium
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.68Medium
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.69Hard
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.70Medium
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.71Hard
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.72Hard
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.73Easy
Which of the following is NOT a character stream class in Java?
Answer: C
FileInputStream is a byte stream class, not a character stream. FileReader, BufferedWriter, and PrintWriter are all character stream classes.
Q.74Medium
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.75Easy
Which interface must be implemented to make an object serializable in Java?
Answer: B
The Serializable interface must be implemented to serialize objects. It is a marker interface with no methods. Classes implementing it can be serialized using ObjectOutputStream.
Q.76Easy
In Java, what is the primary purpose of the flush() method in output streams?
Answer: B
The flush() method forces any buffered output bytes to be written out. It does not close the stream, allowing further writes after flushing.
Q.77Medium
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.78Easy
What will be the output of the following code?
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
Answer: C
The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.
Q.79Medium
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.
Q.80Medium
In a scenario where you need to write formatted output (like printf), which class should be used?
Answer: C
PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.