Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 191–200 of 476
Topics in Java Programming
Q.191 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).

Take Test
Q.192 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.

Take Test
Q.193 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.

Take Test
Q.194 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.

Take Test
Q.195 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.

Take Test
Q.196 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.

Take Test
Q.197 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.

Take Test
Q.198 Medium I/O Streams
In a multi-threaded application, what is a concern when sharing streams between threads?
A Memory leaks
B Data corruption due to concurrent access
C Increased CPU usage
D Automatic buffering issues
Correct Answer:  B. Data corruption due to concurrent access
EXPLANATION

Streams are not thread-safe. Concurrent access by multiple threads can lead to data corruption. Synchronization or separate streams per thread are needed.

Take Test
Q.199 Medium I/O Streams
What exception is thrown when attempting to deserialize an object with a different serialVersionUID?
A IOException
B InvalidClassException
C ClassNotFoundException
D StreamCorruptedException
Correct Answer:  B. InvalidClassException
EXPLANATION

InvalidClassException is thrown when serialVersionUID doesn't match during deserialization, indicating class incompatibility.

Take Test
Q.200 Medium I/O Streams
Consider a scenario where you need to read a line from a file. Which class would be most efficient?
A FileInputStream reading byte by byte
B BufferedReader with readLine() method
C DataInputStream with readUTF()
D FileReader reading character by character
Correct Answer:  B. BufferedReader with readLine() method
EXPLANATION

BufferedReader.readLine() is the most efficient way to read complete lines as it buffers input and provides a dedicated method for line reading.

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