What is the result?

The data.doc, data.txt and data.xml files are accessible and contain text.
Given the code fragment:
Stream<Path> paths = Stream.of (Paths. get(“data.doc”),
Paths. get(“data.txt”),
Paths. get(“data.xml”));
paths.filter(s-> s.toString().endWith(“txt”)).forEach(
s -> {
try {
Files.readAllLines(s)
.stream()
.forEach(System.out::println); //line n1} catch (IOException e) {
System.out.println(“Exception”);
}
}
);
What is the result?

The data.doc, data.txt and data.xml files are accessible and contain text.
Given the code fragment:
Stream<Path> paths = Stream.of (Paths. get(“data.doc”),
Paths. get(“data.txt”),
Paths. get(“data.xml”));
paths.filter(s-> s.toString().endWith(“txt”)).forEach(
s -> {
try {
Files.readAllLines(s)
.stream()
.forEach(System.out::println); //line n1} catch (IOException e) {
System.out.println(“Exception”);
}
}
);
What is the result?

A.
The program prints the content of data.txt file.

B.
The program prints:
Exception
<<The content of the data.txt file>>
Exception

C.
A compilation error occurs at line n1.

D.
The program prints the content of the three files.



Leave a Reply 7

Your email address will not be published. Required fields are marked *


CompileTester

CompileTester

A

smh

smh

need help to understand how it results into answer A.

andrei

andrei

A is correct.
1) paths.filter(s -> s.toString().endsWith(“txt”)) reduce our stream just to one value.
2) forEach is a terminal operation, so stream is proccesed.
3) Files.readAllLines – read all lines from a file.

smh

smh

B.
The program prints:
Exception

DG

DG

It’s likely that you’ve messed smth up at your machine. E.g. the files do not exist or similar.
Assuming, that all the three files exists and accessible, the contents of the txt one would be printed by this code sample.

smh

smh

thank you for your guidance

smh

smh

A.
is correct answer. prints contents of .txt file.