Java SE 11 Developer — Question 26

Given:
String originalPath = “data\\projects\\a-project\\..\\..\\another-project”;
Path path = Paths.get(originalPath);
System.out.print(path.normalize());
What is the result?

Answer options

Correct answer: D

Explanation

The correct answer is D because the normalize() method removes redundant elements from the path, but since '..' indicates going up in the directory structure, it preserves the original representation of the path. Options A and B represent incorrect simplifications of the path, and C shows the path without normalization, which is not the output produced by the normalize() method.