What is the primary difference between throw and throws in Java exception handling?
Answer: B
'throw' is a statement that explicitly throws an exception object. 'throws' is a clause in method signature indicating the method may throw specific checked exceptions. Example: throw new IOException(); vs public void method() throws IOException {}
Q.142Easy
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.143Easy
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.144Easy
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.145Easy
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.146Easy
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.147Easy
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.148Easy
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.149Easy
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.150Easy
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.151Easy
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.152Easy
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.153Easy
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.154Easy
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.155Easy
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.156Easy
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.157Easy
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.158Easy
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.159Easy
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.160Easy
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.