Java OCA OCP Practice Question 2886

Question

If the current working directory is /p1, and the path /p1/data does not exist, then what is the result of executing the following code? (Choose all that apply.).

Path path = Paths.get("data"); 
if(Files.isSameFile(path,Paths.get("/p1/data")))  // x1 
   Files.createDirectory(path.resolve("info"));  // x2 
  • A. The code compiles and runs without issue, but it does not create any directories.
  • B. The directory /p1/data is created.
  • C. The directory /p1/data/info is created.
  • D. The code will not compile because of line x1.
  • E. The code will not compile because of line x2.
  • F. It compiles but throws an exception at runtime.


F.

Note

The code compiles without issue, so D and E are incorrect.

The method Files.isSameFile() first checks to see if the Path values are the same in terms of equals().

Since the first path is relative and the second path is absolute, this comparison will return false, forcing isSameFile() to check for the existence of both paths in the file system.

Since we know /p1/data does not exist, a NoSuchFileException is thrown and F is the correct answer.

A, B, and C are incorrect since an exception is thrown at runtime.




PreviousNext

Related