Which of the following is a character stream class in Java?
Answer: A
FileReader is a character stream class that reads characters from a file. FileInputStream, DataInputStream, and BufferedInputStream are byte stream classes.
Q.2Medium
What is the default buffer size of BufferedReader in Java?
Answer: C
BufferedReader has a default buffer size of 8192 characters, but commonly 2048 bytes is used. The exact implementation may vary, but 8192 is the default character buffer.
Q.3Easy
Which method is used to read a single byte from a FileInputStream?
Answer: A
The read() method reads a single byte and returns it as an integer. Returns -1 if end of stream is reached.
Q.4Medium
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.5Medium
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.
Advertisement
Q.6Easy
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.7Medium
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.8Easy
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.
Q.9Medium
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.10Easy
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.11Easy
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Medium
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.17Hard
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.18Hard
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.19Hard
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.20Medium
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.