nerdexam
Oracle

1Z0-829 · Question #26

Given: public class Test { public String attach1(List<String> data) { return data.parallelStream().reduce("w", (n,m) -> n+m, String::concat); } public String attach2(List<String> data) { return…

The correct answer is E. Compilation fails. Compilation fails because the code uses List<String> as a parameter type and calls List.of(...), but java.util.List is never imported - any class outside java.lang must be explicitly imported, so the compiler rejects the file before any execution occurs. Since the program never…

Working with Streams and Lambda expressions

Question

Given: public class Test { public String attach1(List<String> data) { return data.parallelStream().reduce("w", (n,m) -> n+m, String::concat); } public String attach2(List<String> data) { return data.parallelStream().reduce((p,l) -> p+l).get(); } public static void main(String[] args) { Test t = new Test(); var list = List.of("Table", "Chair"); String x = t.attach1(list); String y = t.attach2(list); System.out.print(x + "+" + y); } } What is the result?

Options

  • ATablechair Tablechair
  • BWTablechair Tablechair
  • CA NullPointerException is thrown
  • DWTablechair TableChair
  • ECompilation fails

How the community answered

(47 responses)
  • A
    21% (10)
  • B
    6% (3)
  • C
    2% (1)
  • D
    9% (4)
  • E
    62% (29)

Explanation

Compilation fails because the code uses List<String> as a parameter type and calls List.of(...), but java.util.List is never imported - any class outside java.lang must be explicitly imported, so the compiler rejects the file before any execution occurs. Since the program never compiles, options A, B, and D (which describe specific runtime string outputs) are impossible, and option C (NullPointerException) is equally impossible for the same reason. Even if you added the missing import, option C would still be wrong because reduce((p,l) -> p+l) on a non-empty list returns a populated Optional<String>, so .get() would not throw; and options A/B/D are wrong because the 3-argument parallel reduce in attach1 applies the identity "w" to each partition independently, so the output would be something like "wTablewChair" - not matching any listed value.

Memory tip: On OCP questions, always scan for types outside java.lang (List, Optional, Map, etc.) before analyzing runtime behavior - if even one is used without an import, the answer is "compilation fails" regardless of how correct the logic looks.

Topics

#Parallel Streams#Stream.reduce()#Type Inference#Lambda Expressions

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice