Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 421–430 of 958
Topics in Java Programming
Q.421 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.

Test
Q.422 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.

Test
Q.423 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.

Test
Q.424 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.

Test
Q.425 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.

Test
Q.426 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.

Test
Q.427 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.

Test
Q.428 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.

Test
Q.429 Medium I/O Streams
What is the serialVersionUID used for in Java serialization?
A To store the Java version number
B To identify the class version for deserialization compatibility
C To encrypt the serialized object
D To track the number of times an object was serialized
Correct Answer:  B. To identify the class version for deserialization compatibility
EXPLANATION

serialVersionUID identifies class versions. Mismatched versions cause InvalidClassException during deserialization.

Test
Q.430 Medium I/O Streams
In a CSV file processing application, why is BufferedReader preferred over FileReader for reading large files?
A BufferedReader automatically parses CSV format
B BufferedReader reduces I/O operations by reading data in chunks
C FileReader cannot read text files
D BufferedReader is faster at character encoding
Correct Answer:  B. BufferedReader reduces I/O operations by reading data in chunks
EXPLANATION

BufferedReader buffers input, reducing system calls and I/O overhead, improving performance significantly.

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