Java OCA OCP Practice Question 2478

Question

What is the output of the following code?

Path path1 = Paths.get("MyDir/hello.java");
Path path2 = Paths.get("FriendDir/code");
Path path3 = path1.relativize(path2);
for (Path path : path3)
    System.out.println(path);
a  ..//  w  w  w. j av  a 2 s  . co m
   ..
   FriendDir
   code

b  ..
   ..
   MyDir
   hello.java

c  FriendDir
   code

d  MyDir
   hello.java

e  ..
   MyDir

f  Compilation error
g  Runtime exception


a

Note

path1.relativize(path2) creates a relative path from path1 to path2.

To navigate from relative path1 MyDir/hello.java to relative path2 FriendDir/code, you need to do the following steps:.

1   (apply ..)-Navigate to the parent directory of MyDir/hello.java-that is, MyDir.
2   (apply ..)-Navigate to the parent directory of MyDir.
3   (apply /FriendDir)-Navigate to /FriendDir.
4   (apply /code)-Navigate to code.

The Path interface extends the Iterable interface.

A Path object can iterate over its name elements using the for-each loop.




PreviousNext

Related