Showing 61–70 of 100 questions
in I/O Streams
Which of the following is a character-based stream in Java?
A
InputStream
B
FileInputStream
C
FileReader
D
DataInputStream
Correct Answer:
C. FileReader
EXPLANATION
FileReader is a character-based stream that reads characters from a file. InputStream and its subclasses are byte-based streams.
What is the purpose of the flush() method in PrintWriter?
A
Close the stream permanently
B
Clear the buffer and write pending data to the underlying stream
C
Reset the file pointer to the beginning
D
Skip unwanted characters from input
Correct Answer:
B. Clear the buffer and write pending data to the underlying stream
EXPLANATION
The flush() method clears the internal buffer and forces any buffered output to be written to the underlying stream without closing it.
Which class in Java I/O streams is used to read primitive data types from a binary file?
A
DataInputStream
B
FileReader
C
BufferedInputStream
D
InputStreamReader
Correct Answer:
A. DataInputStream
EXPLANATION
DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a binary file.
For a real-time log file monitoring application, which approach is most suitable?
A
Use FileInputStream and reload file periodically
B
Use RandomAccessFile to seek to end and read new appended data
C
Use FileWatcher API for detecting changes
D
Load entire file in memory and check periodically
Correct Answer:
B. Use RandomAccessFile to seek to end and read new appended data
EXPLANATION
RandomAccessFile allows seeking to the file's end to read only new appended data efficiently.
When using ObjectInputStream, what exception is thrown if the serialVersionUID doesn't match?
A
SerializationException
B
InvalidClassException
C
ClassNotFoundException
D
IOException
Correct Answer:
B. InvalidClassException
EXPLANATION
InvalidClassException is thrown when deserialized class's serialVersionUID doesn't match the serialized object.
A file contains 1000 lines. Using BufferedReader.readLine() repeatedly reads all lines. What is the time complexity?
A
O(1)
B
O(log n)
C
O(n)
D
O(n²)
EXPLANATION
Reading each line requires processing each character once, resulting in O(n) complexity where n is total characters.
Which scenario would require using a SequenceInputStream?
A
Reading from multiple files sequentially as a single stream
B
Reading very large files
C
Parallel reading from multiple files
D
Converting stream encoding
Correct Answer:
A. Reading from multiple files sequentially as a single stream
EXPLANATION
SequenceInputStream concatenates multiple input streams, allowing them to be read as one continuous stream.
In Java NIO, what is the primary advantage of FileChannel over traditional streams?
A
FileChannel is easier to use
B
FileChannel supports non-blocking I/O and memory-mapped files
C
FileChannel automatically handles buffering
D
FileChannel is compatible with older Java versions
Correct Answer:
B. FileChannel supports non-blocking I/O and memory-mapped files
EXPLANATION
FileChannel enables non-blocking operations and memory-mapping, offering better performance for large files.
What happens when you write to a closed PrintWriter?
A
The data is buffered and written on next open
B
IOException is thrown
C
Data is silently discarded
D
The stream reopens automatically
Correct Answer:
C. Data is silently discarded
EXPLANATION
PrintWriter suppresses IOException and continues silently, unlike other streams.
A program reads a 1 GB binary file and needs to modify specific bytes at random positions. Which class is most suitable?
A
BufferedInputStream with ByteArrayOutputStream
B
FileInputStream with external byte array
C
RandomAccessFile
D
FileInputStream wrapped with DataInputStream
Correct Answer:
C. RandomAccessFile
EXPLANATION
RandomAccessFile allows seeking to any position in the file without sequential reading, ideal for random access.