Java OCA OCP Practice Question 2884

Question

Given that /data is a directory that exists and it is empty, what is the result of the following code?.

Path path = Paths.get("/data"); 
boolean myBoolean = Files.walk(path) 
   .filter((p,a) -> a.isDirectory() && !path.equals(p)) // w1 
   .findFirst().isPresent();  // w2 
System.out.println(myBoolean ? "No Sub-directory": "Has Sub-directory"); 
  • A. It prints No Sub-directory.
  • B. It prints Has Sub-directory.
  • C. The code will not compile because of line w1.
  • D. The code will not compile because of line w2.
  • E. The output cannot be determined.
  • F. It produces an infinite loop at runtime.


C.

Note

The code does not compile since the stream output by Files.

walk() is Stream<Path>, therefore we need a Predictate, not a BiPredicate, on line w1, and the answer is C.

If the Files.find() method had been used instead, and the lambda had been passed as an argument to the method instead of on filter(), the output would be B, Has Sub-directory, since the directory is given to be empty.

For fun, we reversed the expected output of the ternary operation to make sure that you understood the process.




PreviousNext

Related