Oracle Certified Professional Java Se 11 Developer · Free Practice Question Medium

Question 88

Question ID: UKOCP27260


Given code:


F: is accessible for reading and contains 'Book.java' file.


Which of the following statements, if used to replace /*INSERT*/, will successfully print contents of 'Book.java' on to the console?

Choose 3 options.

  • A
  • B
  • C
  • D
Reveal correct answers

Correct answers: A, C, D

Explanation

UKOCP27260:

Below are the declarations of lines and readAllLines methods from Files class: 

public static Stream<String> lines(Path path) throws IOException {...} 

public static List<String> readAllLines(Path path) throws IOException {...} 


Files.lines(Paths.get("F:\\Book.java")) returns Stream<String> object. Hence forEach() can be invoked but stream() can't be invoked.


Files.readAllLines(Paths.get("F:\\Book.java")) returns List<String> object. Hence both forEach() and stream() methods can be invoked. List has both the methods. But converting list to stream() and then invoking forEach() method is not required but it is a legal syntax and prints the file contents.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need