Chapter 8: Lambdas and Funcional Interfaces Flashcards
Functional programming focuses more on expressions than loops.
True
Lambda expressions work with interfaces that have exactly one abstract method.
True
Parentheses around lambda parameters can be omitted if there is only one parameter and its type is not explicitly stated.
True
The lambda expression () -> true is valid and takes zero parameters.
True
A lambda expression must always have an explicit return statement.
(False, it can be omitted for single-expression lambdas.)
Lambda expressions must always declare parameter types explicitly.
(False, type inference is allowed.)
A lambda expression can have multiple abstract methods in its functional interface.
(False, only one abstract method is allowed.)
Lambdas are a feature introduced in Java 14.
(False, they were introduced in Java 8.)
The arrow operator (->) is optional in lambda expressions.
(False, it is mandatory.)
A lambda expression must always have at least one parameter.
(False, it can have zero parameters, e.g., () -> true.)
The lambda (String x) -> X.startsWith(“test”) is correct.
(False, X should be lowercase as x.)
The lambda is valid.
(String x, String y) --> x.startsWith("test")
It’s valid as long as it matches a functional interface with:
* Two String arguments
* A boolean return type
The lambda x, y -> x.startsWith(“fish”) is valid.
(False, parentheses are required for multiple parameters: (x, y) -> x.startsWith(“fish”).)
The lambda x -> { x.startsWith(“camel”); } is valid.
(False, missing return statement inside {}.)
The lambda x -> { return x.startsWith(“giraffe”) } is valid.
(False, missing semicolon inside {}.)
The lambda String x -> x.endsWith(“eagle”) is valid.
(False, missing parentheses around x.)
The lambda expression s -> {}; is invalid.
(False, it is valid as an empty lambda body.)
var invalid = (Animal a) -> a.canHop(); compiles successfully.
(False, var cannot infer the functional interface type.)
Lambdas must be enclosed within a class or method.
“Partially false. While lambdas can be assigned to a variable or passed directly as arguments, they must still be enclosed within a class or method. They cannot exist at the top level outside of a class.”
A lambda with multiple lines of code does not require braces {}.
(False, braces are required for multiple statements.)
What is an unbounded generic type in Java?
An unbounded generic type ‘<’T’>’ can accept any type, with Object as the implicit upper bound.
What is a bounded generic type in Java?
A bounded generic type
(<T extends SomeClass>)restricts T to be a subtype of SomeClass.
What does <’T extends Number’> mean?
T can be any subclass of Number (e.g., Integer, Double, Float), but not String or other non-numeric types.
What does <’T’> mean in Java generics?
It means T can be any type, effectively making it equivalent to <’T extends Object’>.