Java OCA OCP Practice Question 1942

Question

Assuming the current working directory is /2D, what is the expected output of executing the following code snippet?

Path w1 = Paths
          .get("../shape/.././rectangle..") 
          .toAbsolutePath().normalize(); 
System.out.print(w1.resolve("square.txt")); 
  • A. /shape/square.txt
  • B. /2D/rectangle../square.txt
  • C. /rectangle../square.txt
  • D. An exception is printed at runtime.


C.

Note

The toAbsolutePath() combines the current working directory and relative path to form a /2D/../shape/.././rectangle.. path.

The normalize() method removes the path symbols and leaves a /rectangle.. value.

Note that the last double period ( ..) is not removed because it is part of a path name and not interpreted as a path symbol.

The result is then appended with square.txt and we are left with /rectangle../square.txt, making Option C the correct answer.




PreviousNext

Related