Java OCA OCP Practice Question 2882

Question

Assume /data exists as a symbolic link to the directory /source/java within the file system.

Which of the following statements are correct about this code snippet? (Choose all that apply.).

Path path = Paths.get("/data"); 
if(Files.isDirectory(path) && Files.isSymbolicLink(path)) 
   Files.createDirectory(path.resolve("resource")); 
  • A. A new directory will always be created.
  • B. A new directory will be created only if /source/java exists.
  • C. If the code creates a directory, it will be reachable at /data/resource.
  • D. If the code creates a directory, it will be reachable at /source/java/resource.
  • E. The code does not compile.
  • F. The code will compile but always throws an exception at runtime.


B, C, D.

Note

The first clause of the if/then statement will be true only if the target of the symbolic link, /source/java, exists, since by default isDirectory() follows symbolic links, so B is correct.

Option A is incorrect because /source/java may not exist or /source/java/resource may already exist.

If /source/java does exist, then the directory will be created at /source/java/resource, and because the symbolic link would be accessible as /data/resource, C and D are both correct.

E is incorrect, because the code compiles without issue.

F is incorrect because the code may throw an exception at runtime, such as when the file system is unavailable or locked for usage; thus it is not guaranteed to throw an exception at runtime.




PreviousNext

Related