Java OCA OCP Practice Question 1916

Question

Assuming the path referenced below exists and contains a symbolic link that references /again, what is the expected result of executing the following code snippet?

System.out.print(Files.walk(Paths.get("/again/and/again")).count()); 
  • A. An exception is thrown at runtime.
  • B. A number is printed at runtime.
  • C. The process hangs indefinitely.
  • D. The result cannot be determined with the information given.


B.

Note

A cycle is caused when a path contains a symbolic link that references the path itself, or a parent of the parent, triggering an infinitely deep traversal.

That said, Files.walk() does not follow symbolic links by default.

The cycle is never activated, and the code would print a number at runtime, making Option B the correct answer.

If the FOLLOW_LINKS enum value was used in the call to Files.walk(), then it would trigger a cycle resulting in a FileSystemLoopException at runtime, and Option A would be the correct answer.




PreviousNext

Related