OCP Java IO and NIO Flashcards
6 cards from real OCP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 OCP Java IO and NIO flashcards as text
Which interface in Java NIO.2 (introduced in Java 7) represents a path to a file or directory on the file system?
Answer: java.nio.file.Path
java.nio.file.Path is the NIO.2 interface introduced in Java 7 that represents a file system path and largely supersedes java.io.File.
Which two factory methods can be used to create a Path object in Java NIO.2?
Answer: Paths.get() and Path.of()
Paths.get() (available since Java 7) and the static Path.of() method (added in Java 11) are both valid ways to create Path instances.
What does the Files.walk() method return when called with a starting directory Path?
Answer: A lazy Stream traversing the directory tree depth-first
Files.walk() returns a lazily populated Stream that traverses the file tree depth-first from the specified starting directory.
Which StandardWatchEventKinds constant signals that a new file or directory was created in a directory watched by WatchService?
Answer: ENTRY_CREATED
StandardWatchEventKinds.ENTRY_CREATED is the WatchEvent.Kind raised when a new file or directory entry appears in a watched directory.
What is the difference between Path.resolve() and Path.relativize() in Java NIO.2?
Answer: resolve() combines two paths into one; relativize() computes the relative path between two paths
Path.resolve() appends one path to another to form a combined path, while Path.relativize() computes the relative path needed to navigate from one path to another.
Which Files utility method in NIO.2 copies a file from a source Path to a target Path?
Answer: Files.copy()
Files.copy() copies a file from a source Path to a target Path and accepts optional CopyOption flags such as REPLACE_EXISTING.