Given the code fragment:
Path p1 = Paths.get("/Pics/MyPic.jpeg");
System.out.println (p1.getNameCount() +
":" + p1.getName(1) +
":" + p1.getFileName());
Assume that the Pics directory does NOT exist.
What is the result?
Given the code fragment:
Path p1 = Paths.get("/Pics/MyPic.jpeg");
System.out.println (p1.getNameCount() +
":" + p1.getName(1) +
":" + p1.getFileName());
Assume that the Pics directory does NOT exist.
What is the result?
The getNameCount() method returns the number of elements in the path, excluding the root. In the path "/Pics/MyPic.jpeg", there are two elements: "Pics" and "MyPic.jpeg", so p1.getNameCount() returns 2. The getName(1) method retrieves the second element of the path, which is "MyPic.jpeg". Finally, getFileName() returns the name of the file or the last element of the path, which is also "MyPic.jpeg". Despite the directory not existing, these methods do not perform checks for the existence of the path; they only work with the path structure. Therefore, the correct result is 2:Pics:MyPic.jpeg.
Answer is B, even when the directory does not exist.
Yes correct i realized after.
B,tested
I didnt see that pics directory does NOT exist.So answer is A.
B is the correct answer. import java.nio.file.Path; import java.nio.file.Paths; public class PathExample { public static void main(String[] args) { Path p1 = Paths.get("/Pics/MyPic.jpeg"); System.out.println(p1.getNameCount() + ":" + p1.getName(1) + ":" + p1.getFileName()); } }
B is correct.
Root not considered to count -> getName(1) -> MyPic.jpeg
Correct answer is B. If it does not exist, an exception will be thrown when attempting to perform certain operations on the p1 object, such as reading or writing to the file.