Bird
0
0

Given the interface Utils with a static method isEven(int n) that returns true if n is even, how can you use this method inside a class NumberChecker to filter even numbers from a list List<Integer> nums using streams?

hard📝 Application Q15 of 15
Java - Interfaces
Given the interface Utils with a static method isEven(int n) that returns true if n is even, how can you use this method inside a class NumberChecker to filter even numbers from a list List<Integer> nums using streams?
Anums.stream().filter(Utils::isEven).toList();
Bnums.stream().filter(n -> Utils.isEven()).toList();
Cnums.stream().filter(n -> isEven(n)).toList();
Dnums.stream().filter(n -> Utils.isEven(n)).collect();
Step-by-Step Solution
Solution:
  1. Step 1: Understand method reference syntax

    Static methods can be referenced as InterfaceName::methodName in streams.
  2. Step 2: Analyze options for correct syntax

    nums.stream().filter(Utils::isEven).toList(); uses method reference correctly. The lambda n -> Utils.isEven() misses argument n, n -> isEven(n) lacks interface qualification, and .collect() requires a collector.
  3. Final Answer:

    nums.stream().filter(Utils::isEven).toList(); -> Option A
  4. Quick Check:

    Use InterfaceName::staticMethod for stream filters [OK]
Quick Trick: Use InterfaceName::methodName for static method references in streams [OK]
Common Mistakes:
  • Calling static method without argument in lambda
  • Using instance method syntax for static methods
  • Wrong terminal operation like collect() without collector

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes