Java OCA OCP Practice Question 2486

Question

What is the output of the following code?

Path path1 = Paths.get("/pin/./bin/abc/../code/../Hello.java");
System.out.println(path1.normalize());
  • a \pin\bin\abc\Hello.java
  • b \pin\bin\abc\code\Hello.java
  • c \bin\abc\code\Hello.java
  • d \bin\Hello.java
  • e \pin\bin\Hello.java


e

Note

Method normalize() removes the redundancies like " ." (current directory) and " .." (parent directory) from a path.

Because " ." denotes the current directory, it's simply removed by normalize().

" .." is only removed if it is preceded by a non-" .." name.

In the following example, no redundancies are removed:

System.out.println(Paths.get("../../OCPJava7/8.1.txt").normalize());



PreviousNext

Related