Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 20

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?

    Correct Answer: D

    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.

Discussion
HuimOption: B

Answer is B, even when the directory does not exist.

Svetleto13

Yes correct i realized after.

Svetleto13Option: B

B,tested

Svetleto13

I didnt see that pics directory does NOT exist.So answer is A.

asdfjhfgjuaDCVOption: B

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

steefaandOption: B

B is correct.

duydnOption: B

Root not considered to count -> getName(1) -> MyPic.jpeg

r1muka5Option: B

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.