Java OCA OCP Practice Question 2265

Question

Consider the following program:

import java.nio.file.*;

class SubPath {
        public static void main(String []args) {
                 Path aPath = Paths.get("D:\\OCPJP\\programs\\..\\NIO\src\\.\\SubPath.java");
                aPath = aPath.normalize();
                System.out.println(aPath.subpath(2, 3));
        }
}

This program prints the following:.

  • A. ..
  • B. src
  • C. NIO2
  • D. NIO2\src
  • e. ..\NIO2


B.

Note

the normalize() method removes redundant name elements in the given path,

so after the call to the normalize() method, the aPath value is D:\OCPJP\NIO2\src\SubPath.java.

the subpath(int beginIndex, int endIndex) method returns a path based on the values of beginIndex and endIndex.

the name that is closest to the root has index 0; note that the root itself (in this case, D:\) is not considered an element in the path.

hence, the name elements OCPJP, "NIO2", "src", and Subpath.java are in index positions 0, 1, 2, and 3, respectively.

Note that beginIndex is the index of the first element, inclusive of that element; endIndex is the index of the last element, exclusive of that element.

hence, the subpath is src, which is at index position 2 in this path.




PreviousNext

Related