posted 7 years ago
Boyarski, Selikoff OCP
Question 17 on page 502
Assuming the current directory is /animals/cute, which are possible results of executing the following code? (Choose all that apply.)
Files.walk(Paths.get("..").toRealPath().getParent()) // u1
.map(p -> p.toAbsolutePath().toString()) // u2
.filter(s -> s.endsWith(".java")) // u3
.collect(Collectors.toList())
.forEach(System.out::println);
A. It compiles but may throw an exception at runtime.
B. The code will not compile because of line u1.
C. The code will not compile because of line u2.
D. The code will not compile because of line u3.
E. It prints all .java files in the /animals directory tree.
F. It prints all .java files in the /animals/cute directory tree.
G. It prints all .java files in the root directory tree.
Answer:
A, G. The code compiles without issue, so B, C, and D are incorrect. The first line
actually resolves to the root path since .. and getParent()are conceptually equivalent.
Therefore, G is correct and E and F are incorrect. A is also correct since it may encountera file that it does not have access to read, which is common when trying to read an entire file system.
I think E and F are also true, since walk lists the file recursively.
Also I have tested the program and it prints .java fiels in /animals and animals/cute