Return to site

☕🎓JAVA CERTIFICATION QUESTION: Paths first chunk

· java,ocp

What is the output of the following snippet? (Assume the file exists)

Path path = Paths.get("C:\\home\\joe\\foo");

System.out.println(path.getName( 0 ));

* C

* C:

* home

* IllegalArgumentException

* Compilation error

#java #certificationquestion #ocp

https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

Answer: home

getName(0) Returns the path element corresponding to the specified index.

The 0th element is the path element closest to the root.

Path getName(int index)

Returns a name element of this path as a Path object.

The index parameter is the index of the name element to return. The element that is closest to the root in the directory hierarchy has index 0. The element that is farthest from the root has index count-1.

Parameters:

index - the index of the element

Returns:

the name element

Throws:

IllegalArgumentException - if index is negative, index is greater than or equal to the number of elements, or this path has zero name elements