Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 401–410 of 958
Topics in Java Programming
Q.401 Easy I/O Streams
What is the output of the following code?
FileReader fr = new FileReader("test.txt");
int data = fr.read();
System.out.println(data);
A Character from the file
B ASCII value of the character
C -1 if EOF is reached
D File not found exception
Correct Answer:  B. ASCII value of the character
EXPLANATION

FileReader.read() returns the ASCII/Unicode value of the character as an integer, not the character itself. It returns -1 when EOF is reached.

Take Test
Q.402 Easy I/O Streams
Which class in Java is used to read primitive data types from a binary stream?
A DataInputStream
B BufferedInputStream
C FileInputStream
D ObjectInputStream
Correct Answer:  A. DataInputStream
EXPLANATION

DataInputStream is specifically designed to read primitive data types (int, float, double, etc.) from a binary stream using methods like readInt(), readFloat(), etc.

Take Test
Q.403 Hard I/O Streams
For developing a high-performance file copying utility handling files up to 10GB, which approach is optimal?
A Use FileInputStream/FileOutputStream with 1KB buffer
B Use BufferedInputStream/BufferedOutputStream with large buffer size
C Use FileChannel.transferTo() or NIO channels with direct buffers
D Use ObjectInputStream for binary data
Correct Answer:  C. Use FileChannel.transferTo() or NIO channels with direct buffers
EXPLANATION

FileChannel.transferTo() uses OS-level optimizations for file copying, and NIO channels with direct buffers are most efficient for large file transfers.

Take Test
Q.404 Medium I/O Streams
When combining multiple input sources into one logical stream, which class should be used?
A BufferedInputStream
B PushbackInputStream
C SequenceInputStream
D ConcatInputStream
Correct Answer:  C. SequenceInputStream
EXPLANATION

SequenceInputStream concatenates multiple input streams, reading from them sequentially as if they were a single stream.

Take Test
Q.405 Medium I/O Streams
In a file compression utility, which stream would you use for reading compressed data?
A BufferedInputStream
B GZIPInputStream
C DataInputStream
D FileInputStream
Correct Answer:  B. GZIPInputStream
EXPLANATION

GZIPInputStream decompresses GZIP-compressed data, allowing transparent reading of compressed files as normal streams.

Take Test
Q.406 Medium I/O Streams
What is the primary purpose of transient keyword in Java serialization?
A Make objects read-only
B Exclude specific fields from serialization
C Improve serialization performance
D Synchronize object access
Correct Answer:  B. Exclude specific fields from serialization
EXPLANATION

The transient keyword marks fields that should not be serialized, useful for excluding sensitive data or non-serializable objects.

Take Test
Q.407 Medium I/O Streams
For processing delimited text files with variable field counts, which class is most suitable?
A DataInputStream
B BufferedReader with custom parsing logic
C ObjectInputStream
D RandomAccessFile
Correct Answer:  B. BufferedReader with custom parsing logic
EXPLANATION

BufferedReader combined with custom parsing logic (using split or regex) handles variable-length fields better than fixed-format alternatives.

Take Test
Q.408 Hard I/O Streams
Which serialization approach maintains backward compatibility better?
A Updating serialVersionUID each time the class changes
B Never using serialVersionUID
C Keeping serialVersionUID constant and managing version compatibility in readObject()
D Using random serialVersionUID values
Correct Answer:  C. Keeping serialVersionUID constant and managing version compatibility in readObject()
EXPLANATION

Maintaining a constant serialVersionUID and implementing version-aware readObject() methods allows deserializing objects from different class versions.

Take Test
Q.409 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.410 Easy I/O Streams
What happens when you attempt to read from a closed InputStream?
A Returns -1
B Throws IOException
C Returns empty byte array
D Silently ignores the read request
Correct Answer:  B. Throws IOException
EXPLANATION

Attempting to read from a closed stream throws an IOException with message 'Stream closed', preventing accidental data processing from invalid sources.

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