Method References — Concept Check
Questions on Method References. Replace verbose lambdas with concise :: method references — four forms covering static, bound instance, unbound instance, and constructor references.
Questions in this quiz
8 questions on Method References — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In a Java stream pipeline, you want to convert a list of strings to integers and sort them. Which method reference is appropriate for parsing the strings?
- AString::parseInt
- BInteger::toString
- CInteger::parseInt
- DString::valueOf
2.A developer uses the method reference String::toLowerCase in a lambda expression. What does this imply about the lambda's first argument?
- AIt is ignored in the method call.
- BIt must be a StringBuilder object.
- CIt must be a static method.
- DIt is the receiver of the method call.
3.You have a list of Person objects and want to sort them by age using method references. Which method reference should you use?
- APerson::compareTo
- BPerson::equals
- CPerson::getName
- DPerson::age
4.When should you prefer a lambda expression over a method reference in Java?
- AWhen adapting parameter order is necessary.
- BWhen calling a single existing method.
- CWhen the method name is self-documenting.
- DWhen using a static method.
5.A developer uses ArrayList::new in a stream pipeline. What is the purpose of this method reference?
- ATo convert a list to an array.
- BTo call a static method on ArrayList.
- CTo sort elements in the list.
- DTo create a new ArrayList instance.
6.Which method reference form is used in the expression prefix::startsWith?
- AUnbound instance method reference
- BConstructor reference
- CStatic method reference
- DBound instance method reference
7.A developer mistakenly believes that String::isBlank is an unbound instance method reference. What is the correct classification?
- AUnbound instance method reference
- BStatic method reference
- CConstructor reference
- DBound instance method reference
8.You need to negate a condition in a stream filter using method references. Which approach is correct?
- AString::isEmpty.not()
- BPredicate.not(String::isEmpty)
- C!String::isEmpty
- DString::isEmpty.negate()