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.562Medium
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.563Medium
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.564Medium
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.565Medium
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.
Advertisement
Q.566Hard
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.567Medium
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.568Hard
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.569Hard
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.570Easy
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.571Medium
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.572Easy
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.573Easy
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.574Medium
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.575Easy
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.576Medium
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.577Medium
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.
Q.578Medium
What is the difference between read() and read(byte[] b) methods in InputStream?
Answer: A
read() reads and returns a single byte as an int (0-255 or -1 for EOF). read(byte[] b) reads bytes into an array and returns the number of bytes read.
Q.579Medium
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
Answer: B
NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.
Q.580Medium
You need to read a file line by line efficiently. Which approach is best?
Answer: B
BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.