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 11–20 of 27
Topics in Java Programming
Q.11 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.

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

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

Test
Q.14 Easy I/O Streams
Which class would you use to read and write at arbitrary positions in a file?
A BufferedReader
B FileInputStream
C RandomAccessFile
D PushbackInputStream
Correct Answer:  C. RandomAccessFile
EXPLANATION

RandomAccessFile allows reading and writing at arbitrary positions using seek() method, useful for direct access to file contents.

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

FileReader is a character-based stream that reads characters from a file. InputStream and its subclasses are byte-based streams.

Test
Q.16 Easy I/O Streams
What is the purpose of the flush() method in PrintWriter?
A Close the stream permanently
B Clear the buffer and write pending data to the underlying stream
C Reset the file pointer to the beginning
D Skip unwanted characters from input
Correct Answer:  B. Clear the buffer and write pending data to the underlying stream
EXPLANATION

The flush() method clears the internal buffer and forces any buffered output to be written to the underlying stream without closing it.

Test
Q.17 Easy I/O Streams
Which class in Java I/O streams is used to read primitive data types from a binary file?
A DataInputStream
B FileReader
C BufferedInputStream
D InputStreamReader
Correct Answer:  A. DataInputStream
EXPLANATION

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

Test
Q.18 Easy I/O Streams
When using ObjectInputStream, what exception is thrown if the serialVersionUID doesn't match?
A SerializationException
B InvalidClassException
C ClassNotFoundException
D IOException
Correct Answer:  B. InvalidClassException
EXPLANATION

InvalidClassException is thrown when deserialized class's serialVersionUID doesn't match the serialized object.

Test
Q.19 Easy I/O Streams
A RandomAccessFile is opened with mode 'r'. Which operation will throw an exception?
A Reading bytes from the file
B Seeking to a specific position
C Writing bytes to the file
D Closing the file
Correct Answer:  C. Writing bytes to the file
EXPLANATION

Mode 'r' allows read-only access. Write operations will throw IOException.

Test
Q.20 Easy I/O Streams
What will be the output of reading from a ByteArrayInputStream initialized with bytes [65, 66, 67] after calling read() three times?
A 65, 66, 67
B ABC
C A, B, C
D -1
Correct Answer:  A. 65, 66, 67
EXPLANATION

read() returns integer values of bytes. [65, 66, 67] are ASCII values for 'A', 'B', 'C'. Multiple read() calls return these values sequentially.

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