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.2Easy
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.3Easy
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.4Easy
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.5Easy
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.
Advertisement
Q.6Easy
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.7Easy
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.8Easy
What will be the output of reading from a ByteArrayInputStream initialized with bytes [65, 66, 67] after calling read() three times?
Answer: A
read() returns integer values of bytes. [65, 66, 67] are ASCII values for 'A', 'B', 'C'. Multiple read() calls return these values sequentially.
Q.9Easy
A RandomAccessFile is opened with mode 'r'. Which operation will throw an exception?
Answer: C
Mode 'r' allows read-only access. Write operations will throw IOException.
Q.10Easy
Which class in Java I/O streams is used to read primitive data types from a binary file?
Answer: A
DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a binary file.
Q.11Easy
What is the purpose of the flush() method in PrintWriter?
Answer: B
The flush() method clears the internal buffer and forces any buffered output to be written to the underlying stream without closing it.
Q.12Easy
Which of the following is a character-based stream in Java?
Answer: C
FileReader is a character-based stream that reads characters from a file. InputStream and its subclasses are byte-based streams.
Q.13Easy
Which class would you use to read and write at arbitrary positions in a file?
Answer: C
RandomAccessFile allows reading and writing at arbitrary positions using seek() method, useful for direct access to file contents.
Q.14Easy
What happens when you attempt to read from a closed InputStream?
Answer: B
Attempting to read from a closed stream throws an IOException with message 'Stream closed', preventing accidental data processing from invalid sources.
Q.15Easy
Which class in Java is used to read primitive data types from a binary stream?
Answer: A
DataInputStream is specifically designed to read primitive data types (int, float, double, etc.) from a binary stream using methods like readInt(), readFloat(), etc.
Q.16Easy
What is the output of the following code?
FileReader fr = new FileReader("test.txt");
int data = fr.read();
System.out.println(data);
Answer: B
FileReader.read() returns the ASCII/Unicode value of the character as an integer, not the character itself. It returns -1 when EOF is reached.
Q.17Easy
Which interface must be implemented by a class to make its objects serializable?
Answer: B
The Serializable interface is a marker interface that indicates a class can be serialized. It's found in java.io package and requires no methods to be implemented.
Q.18Easy
What does the flush() method do in output streams?
Answer: B
flush() forces any buffered data to be written to the underlying stream immediately, ensuring data is not lost in the buffer.
Q.19Easy
Which class provides buffering capability to improve I/O performance?
Answer: B
BufferedInputStream wraps another InputStream and buffers input, reducing the number of actual read operations and improving performance significantly.
Q.20Easy
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.