Home Subjects Java Programming I/O Streams

Java Programming
I/O Streams

Java OOP, collections, multithreading

53 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 53
Topics in Java Programming
Q.11 Medium I/O Streams
In a scenario where you need to write formatted output (like printf), which class should be used?
A BufferedWriter
B FileWriter
C PrintWriter
D OutputStreamWriter
Correct Answer:  C. PrintWriter
EXPLANATION

PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.

Test
Q.12 Medium I/O Streams
Which of the following best describes the relationship between InputStream and Reader classes?
A Reader extends InputStream
B InputStream extends Reader
C Both extend a common abstract class
D They are independent, parallel hierarchies for byte and character streams
Correct Answer:  D. They are independent, parallel hierarchies for byte and character streams
EXPLANATION

InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.

Test
Q.13 Medium I/O Streams
Which class is used to read primitive data types and strings from a binary stream?
A BufferedInputStream
B DataInputStream
C ObjectInputStream
D SequenceInputStream
Correct Answer:  B. DataInputStream
EXPLANATION

DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.

Test
Q.14 Medium I/O Streams
What is the default buffer size used by BufferedInputStream?
A 512 bytes
B 1024 bytes
C 8192 bytes
D 4096 bytes
Correct Answer:  C. 8192 bytes
EXPLANATION

The default buffer size for BufferedInputStream is 8192 bytes (8 KB). This can be overridden by using the constructor BufferedInputStream(InputStream, int size).

Test
Q.15 Medium I/O Streams
What happens when you create a FileOutputStream with an existing file?
A Throws FileAlreadyExistsException
B Appends to the existing file
C Overwrites the existing file contents
D Merges with existing content
Correct Answer:  C. Overwrites the existing file contents
EXPLANATION

FileOutputStream(String filename) by default overwrites existing file. Use FileOutputStream(File, boolean append) with true to append.

Test
Q.16 Medium I/O Streams
What is the correct way to ensure resources are properly closed in Java 7+?
A Call close() in finally block
B Use try-with-resources statement
C Rely on garbage collector
D Call flush() before closing
Correct Answer:  B. Use try-with-resources statement
EXPLANATION

Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, ensuring proper cleanup even if exceptions occur.

Test
Q.17 Medium I/O Streams
In Java NIO, which class is used for channel-based I/O?
A FileInputStream
B FileChannel
C BufferedInputStream
D DataInputStream
Correct Answer:  B. FileChannel
EXPLANATION

FileChannel provides non-blocking and efficient I/O operations, memory-mapped file access, and file locking capabilities compared to traditional streams.

Test
Q.18 Medium I/O Streams
What does the available() method in InputStream return?
A Number of bytes that can be read without blocking
B Total bytes in the stream
C Remaining bytes in the file
D Whether the stream is open
Correct Answer:  A. Number of bytes that can be read without blocking
EXPLANATION

available() returns an estimate of bytes that can be read without blocking. It doesn't return total file size.

Test
Q.19 Medium I/O Streams
Consider processing a 1GB file. Which approach would be most memory-efficient?
A Read entire file into a byte array
B Use BufferedInputStream with small buffer size
C Use BufferedInputStream with appropriate buffer size and process in chunks
D Use ObjectInputStream to load the whole file
Correct Answer:  C. Use BufferedInputStream with appropriate buffer size and process in chunks
EXPLANATION

Processing large files in chunks using BufferedInputStream with an appropriate buffer size (like 8KB-64KB) balances memory usage and I/O efficiency.

Test
Q.20 Medium I/O Streams
What is the difference between InputStreamReader and FileReader?
A InputStreamReader accepts any InputStream; FileReader is specifically for files
B FileReader is faster than InputStreamReader
C InputStreamReader supports custom character encoding; FileReader doesn't
D Both A and C
Correct Answer:  D. Both A and C
EXPLANATION

InputStreamReader is a bridge from byte streams to character streams and accepts any InputStream with optional charset. FileReader is specifically for file input with default charset.

Test
IGET
IGET AI
Online · Exam prep assistant
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