Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 371–380 of 958
Topics in Java Programming
Q.371 Medium I/O Streams
What happens if you try to read from a closed stream?
A It returns -1
B It returns null
C It throws IOException
D It silently fails
Correct Answer:  C. It throws IOException
EXPLANATION

Attempting to read from a closed stream throws IOException. The stream should be checked and reopened if needed, or proper resource management (try-with-resources) should be used.

Take Test
Q.372 Medium I/O Streams
You need to read a file line by line efficiently. Which approach is best?
A FileInputStream with byte-by-byte reading
B BufferedReader with readLine()
C FileReader with manual character buffering
D Scanner with default buffer
Correct Answer:  B. BufferedReader with readLine()
EXPLANATION

BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.

Take Test
Q.373 Medium I/O Streams
What is the purpose of the serialVersionUID field in a serializable class?
A To identify the version of the JVM
B To ensure version compatibility during deserialization
C To encrypt the serialized data
D To improve serialization speed
Correct Answer:  B. To ensure version compatibility during deserialization
EXPLANATION

serialVersionUID is used to verify that sender and receiver have compatible versions of a Serializable class. If serialVersionUID doesn't match during deserialization, InvalidClassException is thrown.

Take Test
Q.374 Medium I/O Streams
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
A IOException
B NotSerializableException
C SerializationException
D InvalidObjectException
Correct Answer:  B. NotSerializableException
EXPLANATION

NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.

Take Test
Q.375 Medium I/O Streams
What is the difference between read() and read(byte[] b) methods in InputStream?
A read() returns a byte, read(byte[] b) returns multiple bytes in an array
B read(byte[] b) is faster than read()
C read() is for text files, read(byte[] b) is for binary files
D There is no practical difference
Correct Answer:  A. read() returns a byte, read(byte[] b) returns multiple bytes in an array
EXPLANATION

read() reads and returns a single byte as an int (0-255 or -1 for EOF). read(byte[] b) reads bytes into an array and returns the number of bytes read.

Take Test
Q.376 Medium I/O Streams
In a scenario where you need to write formatted output (like printf), which class should be used?
A BufferedWriter
B FileWriter
C PrintWriter
D OutputStreamWriter
Correct Answer:  C. PrintWriter
EXPLANATION

PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.

Take Test
Q.377 Medium I/O Streams
Which of the following best describes the relationship between InputStream and Reader classes?
A Reader extends InputStream
B InputStream extends Reader
C Both extend a common abstract class
D They are independent, parallel hierarchies for byte and character streams
Correct Answer:  D. They are independent, parallel hierarchies for byte and character streams
EXPLANATION

InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.

Take Test
Q.378 Easy I/O Streams
What will be the output of the following code?
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
A The file should be read-only
B The file should be overwritten
C The data should be appended to the existing file
D The file should be encrypted
Correct Answer:  C. The data should be appended to the existing file
EXPLANATION

The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.

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

DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.

Take Test
Q.380 Easy I/O Streams
In Java, what is the primary purpose of the flush() method in output streams?
A To close the stream permanently
B To clear the internal buffer and write pending data
C To reset the stream position to the beginning
D To increase the buffer size
Correct Answer:  B. To clear the internal buffer and write pending data
EXPLANATION

The flush() method forces any buffered output bytes to be written out. It does not close the stream, allowing further writes after flushing.

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