Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

476 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 181–190 of 476
Topics in Java Programming
Q.181 Medium I/O Streams
In the try-with-resources statement, what happens to resources automatically?
A They are flushed but not closed
B They are closed in reverse order of opening
C They are closed in the order they were opened
D They must be manually closed in finally block
Correct Answer:  B. They are closed in reverse order of opening
EXPLANATION

Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.

Take Test
Q.182 Medium I/O Streams
Which class wraps a byte stream to handle character encoding/decoding?
A InputStreamReader
B BufferedReader
C FileReader
D CharArrayReader
Correct Answer:  A. InputStreamReader
EXPLANATION

InputStreamReader is a bridge between byte streams and character streams. It converts bytes to characters using specified character encoding (UTF-8, ISO-8859-1, etc.).

Take Test
Q.183 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.184 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.185 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.186 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.187 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.188 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.189 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.190 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
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