Java Chapter 8 Part 1 Flashcards
(399 cards)
What is a lambda expression in Java?
A lambda expression is an anonymous function (method without a name) that provides a concise way to implement functional interfaces and can be passed around like objects.
What are the four key characteristics of lambda expressions mentioned in the text?
1) Anonymous (no explicit name), 2) Concise (shorter than anonymous classes), 3) Functional (represents behavior), 4) Passable (can be stored in variables or passed to methods)
Given the Animal record:
java Animal animal = new Animal(“Kangaroo”, true, false); CheckTrait hopper = a -> a.canHop(); boolean canHop = hopper.test(animal);
What interface would you need to create to use with the Animal record for trait checking, as shown in the example?
java public interface CheckTrait { boolean test(Animal a); }
How would you rewrite the following class using a lambda expression?
java CheckTrait hopper = a -> a.canHop();
What Java version introduced lambda expressions?
Java 8 introduced lambda expressions.
What is the main advantage of using lambda expressions over traditional class implementations for functional interfaces?
Lambdas provide a more concise syntax by eliminating the need to create entire classes just to encapsulate one piece of functionality.
Given the Animal record and CheckTrait interface, what would be the output of:
The output would be true because the fish can swim (canSwim = true).
What type of programming paradigm do lambda expressions support in Java?
Lambda expressions support functional programming paradigms in Java.
In the context of the Animal example, why is the CheckTrait interface considered a functional interface?
It’s a functional interface because it contains exactly one abstract method (test(Animal a)), which is the requirement for an interface to be used with lambda expressions.
What is the correct syntax for a basic lambda expression in Java?
The basic syntax is: (parameters) -> { body }
Given the lambda expression CheckTrait hopper = a -> a.canHop();, what interface must CheckTrait be to make this valid?
CheckTrait must be a functional interface with exactly one abstract method that takes one parameter and returns a boolean.
What will this code output?
It will output true because the lambda checks if the kangaroo can hop.
Which package contains Java’s built-in functional interfaces like Predicate and Function?
java.util.function
What is the key characteristic that makes an interface a “functional interface” in Java?
It must contain exactly one abstract method.
Which built-in functional interface would you use for an operation that takes one input and returns a boolean result?
Predicate<T></T>
What types of variables can a lambda access from its enclosing scope?
1) Its own parameters, 2) final or effectively final variables from enclosing scope, 3) instance variables.
What are two benefits of using lambda expressions in Java?
1) Conciseness (reduced boilerplate code), 2) Improved readability (clearer intent).
Given this code, what is wrong with the lambda expression?
The lambda is trying to modify count which isn’t final or effectively final - this will cause a compilation error.
Which functional interface would be most appropriate for a operation that takes no input but produces a result?
Supplier<T></T>
What is the equivalent method reference for the lambda expression a -> a.canHop() when implementing the CheckTrait functional interface?
Animal::canHop
Given the following code snippet from the OCA exam, what does the lambda expression a -> a.canSwim() represent?
It represents an implementation of the CheckTrait functional interface that tests if an Animal can swim.
In the context of Java lambdas, what is the purpose of the print() method in this code?
It filters and prints animals from a list based on the behavior defined in the CheckTrait lambda or method reference.
- > listSupplier = ArrayList::new;, what is the equivalent lambda expression?
- >).
- >?