Java OCA OCP Practice Question 1928

Question

What is the output of the following code snippet? Assume that the current directory is the root path.

Path p1 = Paths.get("./locks"); 
Path p2 = Paths.get("/found/data.zip"); 
System.out.println(p1.relativize(p2)); 
System.out.println(p2.relativize(p1)); 
A.   ../found/data.zip /* w w w . ja  va  2  s .co m*/
     ../../locks 

B.   ../../locks 
     ../found/data.zip 

C.   locks/../found/data.zip 
     ../found/locks 

D.   None of the above 


D.

Note

The relativize() method requires that both path values be absolute or relative.

Based on the details provided, p1 is a relative path, while p2 is an absolute path.

The code snippet produces an exception at runtime, making Option D the correct answer.

If the first path was modified to be absolute by dropping the leading dot (.) in the path expression, then the output would match the values in Option A.




PreviousNext

Related