Home Subjects Java Programming I/O Streams

Java Programming
I/O Streams

Java OOP, collections, multithreading

53 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 41–50 of 53
Topics in Java Programming
Q.41 Medium I/O Streams
Which statement about ObjectOutputStream.reset() is correct?
A It resets the file pointer to the beginning
B It clears the internal object graph cache to enable re-serialization of identical objects
C It closes the stream and reopens it
D It deletes the serialized file
Correct Answer:  B. It clears the internal object graph cache to enable re-serialization of identical objects
EXPLANATION

reset() clears ObjectOutputStream's internal cache of serialized objects, allowing duplicate objects to be serialized again.

Test
Q.42 Medium I/O Streams
A serialized object contains a field marked as 'transient'. What happens to this field when the object is deserialized?
A It retains its original value from before serialization
B It is set to its default value (null for objects, 0 for primitives)
C An exception is thrown during deserialization
D It becomes immutable
Correct Answer:  B. It is set to its default value (null for objects, 0 for primitives)
EXPLANATION

Transient fields are excluded from serialization and are initialized to default values upon deserialization.

Test
Q.43 Medium I/O Streams
A Java program needs to read a file containing 10 million integers. Which approach would be most memory-efficient?
A Use FileInputStream with a byte array buffer
B Load entire file into memory using Scanner
C Use DataInputStream with buffering for sequential reading
D Read all bytes at once using Files.readAllBytes()
Correct Answer:  C. Use DataInputStream with buffering for sequential reading
EXPLANATION

DataInputStream with buffering allows sequential reading without loading the entire file into memory, making it ideal for large files.

Test
Q.44 Medium I/O Streams
What is the purpose of the skip() method in InputStream?
A Skip the entire stream
B Skip a specified number of bytes in the stream
C Skip to the end of the stream
D Skip error handling
Correct Answer:  B. Skip a specified number of bytes in the stream
EXPLANATION

The skip(long n) method skips n bytes in the input stream and returns the number of bytes actually skipped.

Test
Q.45 Medium I/O Streams
Which of the following statements will correctly read a file line by line?
A FileInputStream fis = new FileInputStream("file.txt"); String line = fis.readLine();
B BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine();
C FileReader fr = new FileReader("file.txt"); String line = fr.readLine();
D DataInputStream dis = new DataInputStream(new FileInputStream("file.txt")); String line = dis.readLine();
Correct Answer:  B. BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line = br.readLine();
EXPLANATION

BufferedReader's readLine() method is the standard way to read lines. FileInputStream doesn't have readLine(), FileReader doesn't have readLine() either.

Test
Q.46 Medium I/O Streams
What is the mark() method used for in BufferedReader?
A Mark end of stream
B Mark current position in stream for later reset
C Mark data as corrupted
D Mark stream as closed
Correct Answer:  B. Mark current position in stream for later reset
EXPLANATION

The mark() method marks the current position in the stream so it can be returned to later using reset(). Requires markSupported() to return true.

Test
Q.47 Medium I/O Streams
Which class allows reading and writing objects to a stream?
A DataInputStream and DataOutputStream
B ObjectInputStream and ObjectOutputStream
C SerializableInputStream
D ByteArrayStream
Correct Answer:  B. ObjectInputStream and ObjectOutputStream
EXPLANATION

ObjectInputStream and ObjectOutputStream are used for serialization and deserialization of Java objects. Objects must implement Serializable interface.

Test
Q.48 Medium I/O Streams
What is the difference between FileInputStream and BufferedInputStream?
A No difference; they are aliases
B FileInputStream reads directly from file; BufferedInputStream adds buffering for efficiency
C BufferedInputStream reads characters; FileInputStream reads bytes
D FileInputStream is deprecated in Java 11+
Correct Answer:  B. FileInputStream reads directly from file; BufferedInputStream adds buffering for efficiency
EXPLANATION

FileInputStream reads directly from a file byte by byte, while BufferedInputStream wraps another stream and adds buffering to improve performance by reducing I/O operations.

Test
Q.49 Medium I/O Streams
Which of the following creates a bridge between character and byte streams?
A BufferedReader
B InputStreamReader
C DataInputStream
D ObjectInputStream
Correct Answer:  B. InputStreamReader
EXPLANATION

InputStreamReader converts a byte stream (InputStream) into a character stream (Reader), acting as a bridge between the two.

Test
Q.50 Medium I/O Streams
Which class is used to read primitive data types from a stream?
A ObjectInputStream
B DataInputStream
C BufferedInputStream
D InputStreamReader
Correct Answer:  B. DataInputStream
EXPLANATION

DataInputStream provides methods like readInt(), readDouble(), readBoolean() to read primitive data types from a stream.

Test
IGET
IGET AI
Online · Exam prep assistant
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