What happens when you try to read from a closed stream in Java?
Answer: B
Attempting to read from a closed stream throws an IOException. The stream must be open for read operations.
Q.502Medium
Which of the following is a filtered stream?
Answer: B
BufferedOutputStream is a filtered stream that adds buffering capability to an underlying output stream. Filtered streams wrap other streams to add functionality.
Q.503Easy
What is the purpose of the flush() method in output streams?
Answer: B
The flush() method forces any buffered output to be written to the underlying stream immediately without closing it.
Q.504Medium
Which class is used to read primitive data types from a stream?
Answer: B
DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a stream.
Q.505Easy
What is the return type of FileInputStream's read() method?
Answer: B
The read() method returns an int. It returns the byte value (0-255) or -1 if end of stream is reached. This is why it needs to be cast to byte if needed.
Advertisement
Q.506Medium
Which of the following creates a bridge between character and byte streams?
Answer: B
InputStreamReader converts a byte stream (InputStream) into a character stream (Reader), acting as a bridge between the two.
Q.507Easy
What exception is thrown when a file is not found while creating FileInputStream?
Answer: B
FileNotFoundException (a subclass of IOException) is thrown when the specified file does not exist or cannot be opened.
Q.508Easy
Which method reads a complete line from a BufferedReader?
Answer: B
The readLine() method reads a line of text from the BufferedReader, returning a String without the newline character, or null if end of stream is reached.
Q.509Medium
What is the difference between FileInputStream and BufferedInputStream?
Answer: B
FileInputStream reads directly from a file byte by byte, while BufferedInputStream wraps another stream and adds buffering to improve performance by reducing I/O operations.
Q.510Medium
Which class allows reading and writing objects to a stream?
Answer: B
ObjectInputStream and ObjectOutputStream are used for serialization and deserialization of Java objects. Objects must implement Serializable interface.
Q.511Medium
What is the mark() method used for in BufferedReader?
Answer: B
The mark() method marks the current position in the stream so it can be returned to later using reset(). Requires markSupported() to return true.
Q.512Medium
Which of the following statements will correctly read a file line by line?
Answer: B
BufferedReader's readLine() method is the standard way to read lines. FileInputStream doesn't have readLine(), FileReader doesn't have readLine() either.
Q.513Medium
What is the purpose of the skip() method in InputStream?
Answer: B
The skip(long n) method skips n bytes in the input stream and returns the number of bytes actually skipped.
Q.514Hard
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.515Hard
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.516Hard
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.517Medium
A Java program needs to read a file containing 10 million integers. Which approach would be most memory-efficient?
Answer: C
DataInputStream with buffering allows sequential reading without loading the entire file into memory, making it ideal for large files.
Q.518Easy
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.519Medium
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.520Hard
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.