nerdexam
Oracle

1Z0-829 · Question #27

Given: public class Test { public static void main(String[] args) { List<String> elements = Arrays.asList("car", "truck", "car", "bicycle", "car", "truck", "motorcycle"); Map<String, Long> outcome =…

The correct answer is E. Compilation fails. Option E is correct because the code is missing all required import statements - java.util.List, java.util.Arrays, java.util.Map, java.util.stream.Collectors, and java.util.function.Function are not in java.lang and are never auto-imported, so the compiler cannot resolve any of…

Working with Streams and Lambda expressions

Question

Given: public class Test { public static void main(String[] args) { List<String> elements = Arrays.asList("car", "truck", "car", "bicycle", "car", "truck", "motorcycle"); Map<String, Long> outcome = elements.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting() ) ); System.out.println(outcome); } } What is the result?

Options

  • ABicycle=7, car=7, motorcycle=7, truck=7)
  • B3:bicycle, 0:car, 0motorcycle, 5:truck)
  • CBicycle, car, motorcycle, truck)
  • DBicycle=1, car=3, motorcycle=1, truck=2)
  • ECompilation fails.

How the community answered

(26 responses)
  • A
    4% (1)
  • B
    12% (3)
  • C
    4% (1)
  • D
    27% (7)
  • E
    54% (14)

Explanation

Option E is correct because the code is missing all required import statements - java.util.List, java.util.Arrays, java.util.Map, java.util.stream.Collectors, and java.util.function.Function are not in java.lang and are never auto-imported, so the compiler cannot resolve any of these types. Options A, B, and C are nonsensical fabrications that don't reflect how groupingBy with counting() works. Option D has the correct counts (car=3, truck=2, bicycle=1, motorcycle=1) and would be the right output if the code compiled, but it's still wrong here because the code never reaches runtime. The memory tip: always scan for missing imports first - Java exam questions frequently hide compilation errors in code that looks logically correct; if you see collection/stream types with no import block, compilation fails before any logic runs.

Topics

#Collectors#groupingBy#Stream API#Functional interfaces

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice