Java OCA OCP Practice Question 2904

Question

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);

   }
}
  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. 4
  • F. The code throws a runtime exception because of line h1.


B.

Note

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.




PreviousNext

Related