Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 61–70 of 100 questions in I/O Streams
Q.61 Easy 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.

Take Test
Q.62 Easy I/O 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.

Take Test
Q.63 Easy I/O Streams
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.

Take Test
Q.64 Hard I/O Streams
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.

Take Test
Q.65 Easy I/O Streams
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.

Take Test
Q.66 Medium I/O Streams
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²)
Correct Answer:  C. O(n)
EXPLANATION

Reading each line requires processing each character once, resulting in O(n) complexity where n is total characters.

Take Test
Q.67 Medium I/O Streams
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.

Take Test
Q.68 Hard I/O Streams
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.

Take Test
Q.69 Medium I/O Streams
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.

Take Test
Q.70 Hard I/O 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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips