Java OCA OCP Practice Question 2214

Question

What is the output of the following code snippet?

11: Path x = Paths.get(".","p1","..","/note"); 
12: Path y = Paths.get("/p2/d.txt"); 
13: x.normalize(); 
14: System.out.println(x.resolve(y)); 
15: System.out.println(y.relativize(x)); 
A.  /./p1/../note/p2/d.txt //from w ww  .j a v  a  2 s  .c  om
    /p2/d.txt 

B.  /p2/d.txt 
    /p2/d.txt/./p1/../note 

C.  /p2/d.txt 
    /p2/d.txt/note 

D.  /note/p2/d.txt 
     ../p2/d.txt/p1 

E.  The code does not compile. 

F.  The code compiles but an exception is thrown at runtime. 


F.

Note

Line 11 assigns a relative Path value of ./p1/../note to x.

The second line assigns an absolute Path value of /p2/d.txt to y.

Line 13 does not modify the value of x because Path is immutable and x is not reassigned to the new value.

On line 14, the resolve() method is called using y as the input argument.

If the parameter passed to the resolve() method is absolute, then that value is returned, leading the first println() method call to output /p2/d.txt.

On the other hand, the relativize() method on line 15 requires both Path values to be absolute, or both to be relative.

Mixing the two leads to an IllegalArgumentException on line 15 at runtime and makes Option F the correct answer.




PreviousNext

Related