Consider the following code snippet:
Path wordpadPath = Paths.get("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
System.out.println(wordpadPath.subpath(beginIndex, endIndex));
What are the values of the integer values beginIndex
and endIndex
in this program that will result in this code segment printing the string "Program Files" as output?.
a) beginIndex = 1 and endIndex = 2 b) beginIndex = 0 and endIndex = 1 c) beginIndex = 1 and endIndex = 1 d) beginIndex = 4 and endIndex = 16
b)
In the Path class's method subpath
(int beginIndex
, int endIndex
), beginIndex
is the index of the first element (inclusive of that element) and endIndex
is the index of the last element (exclusive of that element).
Note that the name that is closest to the root in the directory hierarchy has index 0.
Here, the string element "Program Files" is the closest to the root C:\, so the value of beginIndex
is 0 and endIndex
is 1.