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

Given the code fragment:

Path file = Paths.get ("courses.txt");

// line n1

courses.txt is accessible.

Assume the -

line n1 to enable the code to print the content of the courses.txt file?

Which code fragment can be inserted at

    Correct Answer: D

    To print the content of the courses.txt file, you can use the Files.lines method which returns a Stream of the lines from the file. Therefore, using Stream<String> fc = Files.lines(file); will be appropriate. This method directly correlates to a Stream, which matches the need of potentially performing operations like forEach for printing to the console. The other options are either syntactically incorrect or misuse the method types (like Files.readAllLines returning a List instead of a Stream).

Discussion
InnovationOption: C

Files.readAllLines returns a list of strings. the correct answer is C

M_JawadOption: D

D is correct with this addition : fc.forEach(n -> System.out.println(n));

M_JawadOption: A

B is incorrect because Files.readAllLines(file) return a list of strings not a Stream

steefaandOption: C

C is correct since readAllLines returns List rather than Stream. For this to be true assume that Files is static imported and there is no need for it to be present in front of readAllLines.

iSnoverOption: C

Files.readAllLines returns a list of strings. The correct answer is C

WilsonKKerllOption: B

Answer is B. Option D is incorrect because it can't print courses.txt, must add fc.forEach(System.out::print).

WilsonKKerll

I mean answer is C.

DestroyerOption: D

The correct answer is D

Ritesh_Option: C

C can be the answer with below modification. Stream<String> fc = Files.readAllLines (file).stream();