Java SE 8 Programmer II — Question 53
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().contains("data")).forEach(
s -> {
try {
Files.readAllLines(s)
.stream()
.forEach(System.out::println); //line n1
} catch (IOException e) {
System.out.println("Exception");
}
}
);
What is the result?
Answer options
- A. The program prints the content of data.txt file.
- B. The program prints: Exception <<The content of the data.txt file>> <<The content of the data.xml file>>
- C. A compilation error occurs at line n1.
- D. The program prints the content of the three files.
Correct answer: A
Explanation
The correct answer is A because the filter condition only matches the path for data.txt, leading to its contents being printed. Options B and D are incorrect since they imply other files or exceptions would be processed, which does not happen. Option C is wrong because there are no compilation errors in the provided code fragment.