Java OCA OCP Practice Question 2618

Question

Consider the following program:

import java.nio.file.*;
import java.util.Iterator;

public class Main {
        public static void main(String[] args) {
                Path aFilePath = Paths.get("D:\\dir\\file.txt");
                Iterator<Path> paths = aFilePath.iterator();
                while(paths.hasNext()) {
                        System.out.print(paths.next() + " ");
                }/*from  w  w w.jav a 2  s  .  co  m*/
        }
}

Assume that the file D:\dir\file.txt does not exist in the underlying file system.

Which of the following options best describes the behavior of this program when it is executed?

  • a) The program throws a FileNotFoundException.
  • b) The program throws an InvalidPathException.
  • c) The program throws an UnsupportedOperationException.
  • d) The program gets into an infinite loop printing "path element: dir" forever.
  • e) The program prints the following: dir file.txt.


e)

Note

The name elements in a path object are identified based on the separators.

Note: To iterate name elements of the Path object does not actually require that the corresponding files/directories must exist, so it will not result in throwing any exceptions.




PreviousNext

Related