Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 81–90 of 100 questions in I/O Streams
Q.81 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.

Take Test
Q.82 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.83 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.84 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
Q.85 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.

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

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

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

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

Take Test
Q.90 Easy I/O Streams
Which method reads a complete line from a BufferedReader?
A read()
B readLine()
C readString()
D nextLine()
Correct Answer:  B. readLine()
EXPLANATION

The readLine() method reads a line of text from the BufferedReader, returning a String without the newline character, or null if end of stream is reached.

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