1Z0-819 I/O Operations 5 — Questions and Answers
Question 1: What is the output of `System.out.printf("%05.2f", 3.7)` ?
- "03.70" (Correct answer)
- "3.700"
- " 3.70"
- "003.7"
Correct answer: "03.70"
`%05.2f` formats to total width 5, 2 decimal places, zero-padded, producing `03.70`.
Question 2: Which `PrintWriter` constructor automatically flushes the buffer after every `println`, `printf`, or `format` call?
- new PrintWriter(writer, true)
- new PrintWriter(writer)
- new PrintWriter(outputStream, true)
- Both A and C (Correct answer)
Correct answer: Both A and C
Passing `true` as the auto-flush argument to either the `Writer` or `OutputStream` constructor enables auto-flush on line-terminating methods.
Question 3: What does `DataInputStream.readUTF()` expect about the byte stream format?
- A 2-byte length prefix followed by modified UTF-8 encoded bytes (Correct answer)
- A null-terminated UTF-8 string
- A BOM followed by UTF-16 bytes
- Pure UTF-8 with no length prefix
Correct answer: A 2-byte length prefix followed by modified UTF-8 encoded bytes
`readUTF` reads a 2-byte big-endian length then the modified UTF-8 bytes, matching the format written by `DataOutputStream.writeUTF`.
Question 4: Which NIO.2 attribute view provides access to POSIX file permissions?
- PosixFileAttributeView (Correct answer)
- BasicFileAttributeView
- AclFileAttributeView
- DosFileAttributeView
Correct answer: PosixFileAttributeView
`PosixFileAttributeView` exposes owner, group, and permission bits on POSIX-compliant filesystems.
Question 5: What is the default behavior of `Files.copy(InputStream in, Path target)` if the target file already exists?
- It throws `FileAlreadyExistsException` (Correct answer)
- It overwrites the target silently
- It appends to the target
- It creates a numbered backup
Correct answer: It throws `FileAlreadyExistsException`
Without `REPLACE_EXISTING`, `Files.copy` fails with `FileAlreadyExistsException` to avoid silent data loss.
Question 6: Which method on `BufferedWriter` writes a platform-dependent newline?
- newLine() (Correct answer)
- writeLine()
- println()
- flush()
Correct answer: newLine()
`BufferedWriter.newLine()` writes the system property `line.separator`, making output portable across platforms.
Question 7: What does calling `mark(readAheadLimit)` on a `BufferedInputStream` allow you to do?
- Reset the stream position back to the marked point after reading up to `readAheadLimit` bytes (Correct answer)
- Seek to a specific byte offset in the underlying file
- Mark the stream as read-only
- Flush the internal buffer up to the marked position
Correct answer: Reset the stream position back to the marked point after reading up to `readAheadLimit` bytes
`mark` saves the current position; `reset` returns to it as long as no more than `readAheadLimit` bytes have been read since the mark.
What is the output of `System.out.printf("%05.2f", 3.7)` ?