Home Subjects Java Programming I/O Streams

Java Programming
I/O Streams

Java OOP, collections, multithreading

27 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–27 of 27
Topics in Java Programming
Q.21 Easy I/O Streams
Which of the following correctly demonstrates chaining multiple filters in Java I/O?
A new BufferedReader(new FileReader(file))
B new FileReader(new BufferedReader(file))
C new Reader(new BufferedReader(new FileReader(file)))
D new BufferedInputStream(new FileInputStream(new File(file)))
Correct Answer:  A. new BufferedReader(new FileReader(file))
EXPLANATION

Filter streams are chained from inside-out. FileReader is wrapped with BufferedReader for better performance.

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

Test
Q.23 Easy I/O Streams
What exception is thrown when a file is not found while creating FileInputStream?
A NullPointerException
B FileNotFoundException
C IOException
D StreamException
Correct Answer:  B. FileNotFoundException
EXPLANATION

FileNotFoundException (a subclass of IOException) is thrown when the specified file does not exist or cannot be opened.

Test
Q.24 Easy I/O Streams
What is the return type of FileInputStream's read() method?
A byte
B int
C char
D void
Correct Answer:  B. int
EXPLANATION

The read() method returns an int. It returns the byte value (0-255) or -1 if end of stream is reached. This is why it needs to be cast to byte if needed.

Test
Q.25 Easy I/O Streams
What is the purpose of the flush() method in output streams?
A Close the stream
B Clear the buffer and write pending data to the underlying stream
C Reset the stream position
D Release memory allocated to the stream
Correct Answer:  B. Clear the buffer and write pending data to the underlying stream
EXPLANATION

The flush() method forces any buffered output to be written to the underlying stream immediately without closing it.

Test
Q.26 Easy I/O Streams
Which method is used to read a single byte from a FileInputStream?
A read()
B readByte()
C readSingleByte()
D nextByte()
Correct Answer:  A. read()
EXPLANATION

The read() method reads a single byte and returns it as an integer. Returns -1 if end of stream is reached.

Test
Q.27 Easy I/O Streams
Which of the following is a character stream class in Java?
A FileReader
B FileInputStream
C DataInputStream
D BufferedInputStream
Correct Answer:  A. FileReader
EXPLANATION

FileReader is a character stream class that reads characters from a file. FileInputStream, DataInputStream, and BufferedInputStream are byte stream classes.

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