Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 381–390 of 958
Topics in Java Programming
Q.381 Easy I/O Streams
Which interface must be implemented to make an object serializable in Java?
A Cloneable
B Serializable
C Comparable
D Iterable
Correct Answer:  B. Serializable
EXPLANATION

The Serializable interface must be implemented to serialize objects. It is a marker interface with no methods. Classes implementing it can be serialized using ObjectOutputStream.

Take Test
Q.382 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.383 Easy I/O Streams
Which of the following is NOT a character stream class in Java?
A FileReader
B BufferedWriter
C FileInputStream
D PrintWriter
Correct Answer:  C. FileInputStream
EXPLANATION

FileInputStream is a byte stream class, not a character stream. FileReader, BufferedWriter, and PrintWriter are all character stream classes.

Take Test
Q.384 Hard I/O Streams
In a log aggregation system, what is the primary advantage of using RandomAccessFile over sequential streams?
A It's always faster
B It allows seeking to specific positions without reading all data sequentially
C It supports threading automatically
D It automatically compresses data
Correct Answer:  B. It allows seeking to specific positions without reading all data sequentially
EXPLANATION

RandomAccessFile supports seek() operation to jump to specific file positions, useful for reading log tail or accessing specific records directly.

Take Test
Q.385 Hard I/O Streams
Which approach would you use to read and write objects to a file efficiently in a production application?
A Manually serialize each field with DataOutputStream
B Use ObjectInputStream/ObjectOutputStream with proper version control
C Convert objects to JSON and use FileWriter
D Create custom byte arrays for each object
Correct Answer:  B. Use ObjectInputStream/ObjectOutputStream with proper version control
EXPLANATION

ObjectInputStream/ObjectOutputStream with serialVersionUID ensures compatibility, versioning, and is the standard approach for object persistence in Java.

Take Test
Q.386 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.387 Hard I/O Streams
Given the code: PrintWriter pw = new PrintWriter(new FileWriter("file.txt")); What issue might occur?
A FileWriter is not suitable for text files
B Data might not be written to disk if flush() is not called before closing
C PrintWriter cannot wrap FileWriter
D Encoding issues will definitely occur
Correct Answer:  B. Data might not be written to disk if flush() is not called before closing
EXPLANATION

PrintWriter uses buffering by default. If flush() or close() is not called, buffered data may not be written to disk.

Take Test
Q.388 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.389 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.390 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
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