Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 411–420 of 958
Topics in Java Programming
Q.411 Hard I/O Streams
For encrypting data while writing to a file, which approach is most appropriate?
A Use FileWriter directly with encoded strings
B Wrap FileOutputStream with CipherOutputStream
C Use PrintWriter with encryption library
D Convert data to hexadecimal before writing
Correct Answer:  B. Wrap FileOutputStream with CipherOutputStream
EXPLANATION

CipherOutputStream allows transparent encryption of data written through it, properly integrating with Java's cryptography framework.

Test
Q.412 Medium I/O Streams
Which method of PushbackInputStream allows you to return bytes to the stream to be read again?
A reset()
B unread()
C rewind()
D goBack()
Correct Answer:  B. unread()
EXPLANATION

The unread() method of PushbackInputStream allows you to push bytes back into the stream so they can be read again.

Test
Q.413 Medium I/O Streams
In a multi-threaded application, which stream class is thread-safe?
A FileInputStream
B BufferedReader
C PrintWriter
D FileOutputStream
Correct Answer:  C. PrintWriter
EXPLANATION

PrintWriter is synchronized, making it thread-safe. Other stream classes are not inherently thread-safe and require external synchronization in multi-threaded contexts.

Test
Q.414 Medium I/O Streams
What is the default charset used by InputStreamReader if none is specified?
A UTF-8
B ISO-8859-1
C System default charset
D ASCII
Correct Answer:  C. System default charset
EXPLANATION

If no charset is explicitly provided, InputStreamReader uses the platform's default charset, which varies by system configuration.

Test
Q.415 Medium I/O Streams
When processing a large XML file, which approach is most memory-efficient?
A Load entire file into memory using FileInputStream
B Use BufferedReader to read line-by-line
C Use SAX parser or streaming XML parser
D Convert file to ObjectInputStream
Correct Answer:  C. Use SAX parser or streaming XML parser
EXPLANATION

SAX parsers and streaming XML parsers process XML sequentially without loading the entire document, making them ideal for large files.

Test
Q.416 Easy I/O Streams
Which class would you use to read and write at arbitrary positions in a file?
A BufferedReader
B FileInputStream
C RandomAccessFile
D PushbackInputStream
Correct Answer:  C. RandomAccessFile
EXPLANATION

RandomAccessFile allows reading and writing at arbitrary positions using seek() method, useful for direct access to file contents.

Test
Q.417 Medium I/O Streams
In a real-time application processing sensor data streams, which buffering approach would minimize latency?
A Increase buffer size to 64KB
B Use unbuffered I/O or very small buffer sizes with frequent flush()
C Use ObjectInputStream for all data
D Implement custom byte arrays
Correct Answer:  B. Use unbuffered I/O or very small buffer sizes with frequent flush()
EXPLANATION

For real-time applications requiring minimal latency, smaller buffer sizes and frequent flushing ensure data is processed immediately rather than waiting for buffer to fill.

Test
Q.418 Medium I/O Streams
What exception is thrown when trying to deserialize an object that was serialized with a different serialVersionUID?
A ClassCastException
B InvalidClassException
C StreamCorruptedException
D NotSerializableException
Correct Answer:  B. InvalidClassException
EXPLANATION

InvalidClassException is thrown when a serialized object's serialVersionUID doesn't match the current class definition, indicating an incompatible class version.

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

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

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