Java OCA OCP Practice Question 1932

Question

What is the output of the following application? Assume the application is called with a valid path that exists and is accessible within the file system.

package mypkg; //w  w  w  . j  a  v a 2  s .c  om
import java.nio.file.*; 
public class Main { 
   protected void printMain(Path p) { 
      for(Path f : Files.list(p)) {  // n1 
         if(f.toString().endsWith(".per"))  // n2 
            System.out.print(f); 
      } 
   } 
   public static void main(String... volunteers) { 
      new Main().printMain(Paths.get(volunteers[0])); 
   } 
} 
  • A. A list of file names is printed at runtime.
  • B. The class does not compile due to line n1.
  • C. The class does not compile due to line n2.
  • D. None of the above


B.

Note

The code does not compile because Files.list() returns a Stream<Path>, not a List<Path>, making Option B the correct answer.

Note that java.io.File does include a list() method that returns an array of String values and a listFiles() method that returns an array of File values, but neither is applicable here.




PreviousNext

Related