Assuming the current directory is /seals/harp/food, what is the result of executing the following code?
import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws Exception { final Path path = Paths.get(".").normalize(); // h1 int count = 0; for (int i = 0; i < path.getNameCount(); ++i) { count++;//from ww w . jav a 2s .c o m } System.out.println(count); } }
B.
The normalize()
method does not convert a relative path into an absolute path; therefore, the path value after the first line is just the current directory symbol.
The for()
loop iterates the name values, but since there is only one entry, the loop terminates after a single iteration.
Therefore, B is correct and the rest of the answers are incorrect.