Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 431–440 of 958
Topics in Java Programming
Q.431 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.432 Medium I/O Streams
When implementing Externalizable interface, which method is mandatory?
A serialize()
B writeObject() and readObject()
C writeExternal() and readExternal()
D write() and read()
Correct Answer:  C. writeExternal() and readExternal()
EXPLANATION

Externalizable requires implementation of writeExternal() and readExternal() for complete control over serialization.

Test
Q.433 Medium I/O Streams
Which statement about ObjectOutputStream.reset() is correct?
A It resets the file pointer to the beginning
B It clears the internal object graph cache to enable re-serialization of identical objects
C It closes the stream and reopens it
D It deletes the serialized file
Correct Answer:  B. It clears the internal object graph cache to enable re-serialization of identical objects
EXPLANATION

reset() clears ObjectOutputStream's internal cache of serialized objects, allowing duplicate objects to be serialized again.

Test
Q.434 Hard I/O Streams
A developer uses PipedInputStream and PipedOutputStream in the same thread. What will happen?
A The program will execute successfully
B A deadlock will occur because both read and write happen in the same thread
C The pipe will be closed automatically
D An IOException will be thrown immediately
Correct Answer:  B. A deadlock will occur because both read and write happen in the same thread
EXPLANATION

Pipes require separate threads for reading and writing. Using both in the same thread causes deadlock.

Test
Q.435 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
Q.436 Hard I/O Streams
In a multi-threaded environment, which stream class provides thread-safe read/write operations without external synchronization?
A PrintStream
B FileOutputStream
C BufferedOutputStream
D PipedInputStream
Correct Answer:  A. PrintStream
EXPLANATION

PrintStream (used by System.out) is internally synchronized for thread-safety, unlike other stream classes.

Test
Q.437 Medium I/O Streams
A serialized object contains a field marked as 'transient'. What happens to this field when the object is deserialized?
A It retains its original value from before serialization
B It is set to its default value (null for objects, 0 for primitives)
C An exception is thrown during deserialization
D It becomes immutable
Correct Answer:  B. It is set to its default value (null for objects, 0 for primitives)
EXPLANATION

Transient fields are excluded from serialization and are initialized to default values upon deserialization.

Test
Q.438 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.439 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.

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

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