1Z0-819 I/O Operations 4 — Questions and Answers
Question 1: Which class is used for high-performance binary file I/O using memory-mapped files in Java NIO?
- MappedByteBuffer (Correct answer)
- FileChannel
- ByteBuffer
- FileInputStream
Correct answer: MappedByteBuffer
`MappedByteBuffer` maps a region of a file directly into memory, enabling fast random access without traditional read/write calls.
Question 2: What is the purpose of `FileChannel.transferTo(position, count, target)`?
- Efficiently transfers bytes from one channel to another, potentially using OS-level zero-copy (Correct answer)
- Reads a file into a byte array
- Writes a ByteBuffer to a file
- Creates a new file channel from an existing one
Correct answer: Efficiently transfers bytes from one channel to another, potentially using OS-level zero-copy
`transferTo` can leverage OS zero-copy mechanisms to move data between channels without buffering in the JVM heap.
Question 3: Which interface must be implemented to receive notifications from a `WatchService`?
- No interface — you poll `WatchKey` events from the service (Correct answer)
- WatchListener
- FileChangeListener
- Watchable
Correct answer: No interface — you poll `WatchKey` events from the service
`WatchService` is poll-based: you call `take()` or `poll()` on the service to retrieve `WatchKey` objects containing `WatchEvent` lists.
Question 4: What does `Serializable` marker interface indicate for Java I/O?
- The class can be serialized by `ObjectOutputStream` and deserialized by `ObjectInputStream` (Correct answer)
- The class can be written to a `PrintStream`
- The class supports `DataOutputStream` writing
- The class implements a custom `readObject` method
Correct answer: The class can be serialized by `ObjectOutputStream` and deserialized by `ObjectInputStream`
Implementing `Serializable` signals that `ObjectOutputStream`/`ObjectInputStream` may serialize and deserialize instances of the class.
Question 5: What is the role of `serialVersionUID` in serialization?
- It is used to verify that the sender and receiver of a serialized object have loaded classes that are compatible (Correct answer)
- It determines the byte order of serialized data
- It specifies which fields to include in serialization
- It prevents the class from being serialized
Correct answer: It is used to verify that the sender and receiver of a serialized object have loaded classes that are compatible
During deserialization, the JVM compares `serialVersionUID` between the class definition and the stream; a mismatch throws `InvalidClassException`.
Question 6: Which keyword prevents a field from being serialized by `ObjectOutputStream`?
- transient (Correct answer)
- volatile
- static
- final
Correct answer: transient
Fields marked `transient` are skipped by the default serialization mechanism and restored to their default values on deserialization.
Question 7: What does `Console.readPassword()` return that makes it preferable to `readLine()` for passwords?
- A `char[]` that can be zeroed out after use, unlike `String` which is immutable (Correct answer)
- An encrypted `String`
- A `SecretKey` object
- A `byte[]` of the raw keystrokes
Correct answer: A `char[]` that can be zeroed out after use, unlike `String` which is immutable
Returning `char[]` allows the caller to overwrite the array in memory after use, while `String` values persist in the string pool.
Which class is used for high-performance binary file I/O using memory-mapped files in Java NIO?