1Z0-809 · Question #230
Given that these files exist and are accessible: /company/emp/info.txt /company/emp/benefits/b1.txt and given the code fragment: ``java // line n1 stream.forEach(s -> System.out.print(s)); ` Which…
The correct answer is B. Stream<Path> stream = Files.find( Paths.get("/company"), 1, (p,b) -> p.isDirectory(), FileVisitOption.FOLLOW_LINKS). Files.find() with maxDepth=1 limits traversal to one level beneath /company, and the BiPredicate (p,b) -> b.isDirectory() filters results to directories only - making /company/emp the sole match. Option A (Files.list("/company")) returns all direct entries in /company without…
Question
/company/emp/info.txt
/company/emp/benefits/b1.txt
and given the code fragment:
// line n1
stream.forEach(s -> System.out.print(s));
Which code fragment can be inserted at line n1 to enable the code to print only /company/emp?Options
- AStream<Path> stream = Files.list(Paths.get("/company"));
- BStream<Path> stream = Files.find( Paths.get("/company"), 1, (p,b) -> p.isDirectory(), FileVisitOption.FOLLOW_LINKS);
- CStream<Path> stream = Files.walk(Paths.get("/company"));
- DStream<Path> stream = Files.list(Paths.get("/company/emp"));
How the community answered
(51 responses)- A14% (7)
- B75% (38)
- C8% (4)
- D4% (2)
Explanation
Files.find() with maxDepth=1 limits traversal to one level beneath /company, and the BiPredicate (p,b) -> b.isDirectory() filters results to directories only - making /company/emp the sole match. Option A (Files.list("/company")) returns all direct entries in /company without any predicate, so any non-directory siblings of emp would also be printed. Option C (Files.walk("/company")) recursively traverses the entire subtree with no depth limit, printing every path including info.txt, benefits/, and b1.txt. Option D (Files.list("/company/emp")) lists the contents inside /company/emp - its children info.txt and benefits/ - not the path /company/emp itself.
Memory tip: Think of it as three verbs with increasing power - list (flat, no filter), walk (unlimited recursion, no filter), find (controlled depth + predicate). Any time the exam wants a specific type of path (directory, regular file) at a bounded depth, the answer almost always involves Files.find().
Community Discussion
No community discussion yet for this question.