Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

212 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 91–100 of 212
Topics in Java Programming
Q.91 Hard I/O Streams
In Java NIO, what is a key advantage of FileChannel over traditional I/O streams?
A It automatically handles encoding
B It supports memory mapping and direct buffer access
C It doesn't require closing
D It's easier to use than streams
Correct Answer:  B. It supports memory mapping and direct buffer access
EXPLANATION

FileChannel supports memory-mapped files and direct buffer manipulation, enabling high-performance I/O operations not possible with traditional streams.

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

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

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

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

Take Test
Q.96 Hard I/O Streams
A developer uses PipedInputStream and PipedOutputStream in the same thread. What will happen?
A The program will execute successfully
B A deadlock will occur because both read and write happen in the same thread
C The pipe will be closed automatically
D An IOException will be thrown immediately
Correct Answer:  B. A deadlock will occur because both read and write happen in the same thread
EXPLANATION

Pipes require separate threads for reading and writing. Using both in the same thread causes deadlock.

Take Test
Q.97 Hard I/O Streams
In a multi-threaded environment, which stream class provides thread-safe read/write operations without external synchronization?
A PrintStream
B FileOutputStream
C BufferedOutputStream
D PipedInputStream
Correct Answer:  A. PrintStream
EXPLANATION

PrintStream (used by System.out) is internally synchronized for thread-safety, unlike other stream classes.

Take Test
Q.98 Hard I/O Streams
Which of the following is true about serialization in Java?
A All classes are serializable by default
B A class must implement Serializable interface to be serialized
C Serialization is only for primitive types
D Static fields are always serialized
Correct Answer:  B. A class must implement Serializable interface to be serialized
EXPLANATION

A class must explicitly implement the Serializable interface to be serialized. Static fields are not serialized, and not all classes are serializable by default.

Take Test
Q.99 Hard I/O Streams
What happens if you call close() multiple times on a stream?
A Throws exception on second call
B First call closes; subsequent calls are typically ignored
C Causes memory leak
D Reopens the stream
Correct Answer:  B. First call closes; subsequent calls are typically ignored
EXPLANATION

Most streams in Java are idempotent regarding close() - calling it multiple times is safe and subsequent calls typically do nothing.

Take Test
Q.100 Hard I/O Streams
In Java NIO, which class replaces traditional Stream-based I/O for better performance?
A FastInputStream
B ByteBuffer and Channels
C HighPerformanceStream
D AsyncInputStream
Correct Answer:  B. ByteBuffer and Channels
EXPLANATION

Java NIO (New I/O) uses ByteBuffer and Channels for non-blocking, scalable I/O operations with better performance than traditional streams.

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