Java OCA OCP Practice Question 312

Question

Given a directory tree at the root of the C: drive and the fact that no other files exist:

dir x - |
..........| - dir y
..........| - file a

and these two paths:

Path one = Paths.get("c:/x");
Path two = Paths.get("c:/x/y/a");

Which of the following statements prints out: y/a ?

  • A. System.out.println(one.relativize(two));
  • B. System.out.println(two.relativize(one));
  • C. System.out.println(one.resolve(two));
  • D. System.out.println(two.resolve(one));
  • E. System.out.println(two.resolve(two));
  • F. None of the above


A is correct because it prints the path to get to two from one.

Note

B is incorrect because it prints out ../.. which is the path to navigate to one from two.

This is the reverse of what we want.

C, D, and E are incorrect because it does not make sense to call resolve with absolute paths.

They might print out c:/x/c:/x/y/a, c:/x/y/a/c:/x, and c:/x/y/a/c:/x/y/a, respectively.

F is incorrect because of the above.

Note that the directory structure provided is redundant.

Neither relativize() nor resolve() requires either path to actually exist.




PreviousNext

Related