Java OCA OCP Practice Question 1946

Question

What is the output of the following code snippet?

Assume all referenced paths exist within the file system.

Path v1 = Path.get("/./p1/./").resolve(Paths.get("data.doc")); 
Path v2 = new File("/p1/./p2/../data.doc").toPath(); 
System.out.print(Files.isSameFile(v1,v2)); 
System.out.print(" "+v1.equals(v2)); 
System.out.print(" "+v1.normalize().equals(v2.normalize())); 
  • A. false false false
  • B. true false true
  • C. true true true
  • D. None of the above


D.

Note

The code does not compile because Path is an interface and does not contain a get() method.

Since the first line contains a compilation error, Option D is the correct answer.

If the code was corrected to use Paths.get(), then the output would be true false true, and Option B would be the correct answer.

The normalized path of both is /p1/data.doc, which means they would be equivalent, in terms of equals(), and point to the same path in the file system.

On the other hand, the non-normalized values are not equivalent, in terms of equals(), since the objects represent distinct path values.




PreviousNext

Related