nerdexam
Oracle

1Z0-809 · Question #85

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…

The correct answer is A. The program prints the content of data.txt file. Option A is correct because the filter() operation acts as a gate - only data.txt satisfies .endsWith("txt"), so data.doc and data.xml never reach the forEach. Since the question states data.txt is accessible and contains text, Files.readAllLines(s) succeeds without throwing an…

Question

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().endsWith("txt")).forEach ( s -> { Files.readAllLines(s) .stream() .forEach (System.out::println); //line n1 } catch (IOException e) { System.out.println ("Exception"); } ); What is the result?

Options

  • AThe program prints the content of data.txt file.
  • BThe program prints: Exception <<The Content of the data.txt file>> Exception
  • CA compilation error occurs at line n1.
  • DThe program prints the content of the three files.

How the community answered

(38 responses)
  • A
    74% (28)
  • B
    5% (2)
  • C
    16% (6)
  • D
    5% (2)

Explanation

Option A is correct because the filter() operation acts as a gate - only data.txt satisfies .endsWith("txt"), so data.doc and data.xml never reach the forEach. Since the question states data.txt is accessible and contains text, Files.readAllLines(s) succeeds without throwing an IOException, and each line is printed via System.out::println.

Option B is wrong because no IOException is thrown - the file is explicitly stated to be accessible, so the catch block never executes; there are no "Exception" prints surrounding the content.

Option C is wrong because line n1 is syntactically and semantically valid Java - Files.readAllLines(s).stream().forEach(System.out::println) is a legitimate chain, and the IOException is properly handled by the surrounding try-catch, satisfying the compiler's checked exception requirement.

Option D is wrong because the filter predicate eliminates data.doc and data.xml before any file I/O occurs - only one path survives the stream pipeline.

Memory tip: Think of Stream.filter() as a bouncer at the door - it decides who gets in before any work happens. When you see a filter in a stream question, mentally cross out the rejected elements immediately; the remaining operations only ever see the survivors.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice