Java SE 8 Programmer II — Question 113

Given the code fragment:
Path path1 = Paths.get("/app/./sys/");
Path res1 = path1.resolve("log");
Path path2 = Paths.get("/server/exe/");
Path res2 = path2.resolve("/readme/");
System.out.println(res1);
System.out.println(res2);
What is the result?

Answer options

Correct answer: B

Explanation

The correct answer is B because the resolve method appends the given path to the existing path. In the case of 'path1', the './' does not change the final output, leading to '/app/log/sys'. For 'path2', resolving an absolute path '/readme/' results in '/server/exe/readme'. Other options misrepresent these resolutions.