Java OCA OCP Practice Question 2526

Question

Assuming that the directory /gorilla exists within the file system with the numerous files including signed-words.txt, what is the result of executing the following code? (Choose all that apply.)

Path path = Paths.get("/gorilla/signed-words.txt"); 
? 
Files.find(path.getParent(),10.0,  // k1 
   (Path p) -> p.toString().endsWith(".txt") && Files.isDirectory(p)) // k2 
   .collect(Collectors.toList()) 
   .forEach(System.out::println); 
? 
Files.readAllLines(path) // k3 
   .flatMap(p -> Stream.of(p.split(" "))) // k4 
   .map(s -> s.toLowerCase()) // k5 
   .forEach(System.out::println); 
  • A. The code compiles but does not produce any output at runtime.
  • B. It does not compile because of line k1.
  • C. It does not compile because of line k2.
  • D. It does not compile because of line k3.
  • E. It does not compile because of line k4.
  • F. The code prints all of the .txt files in the directory tree.
  • G. The code prints all of the words in the signed-words.txt file, each on a different line.


B, C, E.

Note

Numerous lines would have to be corrected for the code to compile, so A, F, and G are incorrect.

First off, the second parameter to Files.find() is the depth limit and must be an int, so line k1 would have to be changed to make the code compile, and B is correct.

Next, the Files.find() method uses a BiPredictate<Path,BasicFileAttribute>, not a Predicate<Path>, for its lambda expression, so line k2 would also need to be changed to allow the code to compile, and C is also correct.

Finally, Files.readAllLines() returns a List<String>, not a stream, so the following line, k4, which applies flatMap(), would also prevent the code from compiling, and E is correct.

D is incorrect, since line k3 by itself does not cause any compilation issues.




PreviousNext

Related