Chapter 8: Lambdas and Funcional Interfaces Flashcards

1
Q

Functional programming focuses more on expressions than loops.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lambda expressions work with interfaces that have exactly one abstract method.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Parentheses around lambda parameters can be omitted if there is only one parameter and its type is not explicitly stated.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The lambda expression () -> true is valid and takes zero parameters.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A lambda expression must always have an explicit return statement.

A

(False, it can be omitted for single-expression lambdas.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Lambda expressions must always declare parameter types explicitly.

A

(False, type inference is allowed.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

A lambda expression can have multiple abstract methods in its functional interface.

A

(False, only one abstract method is allowed.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Lambdas are a feature introduced in Java 14.

A

(False, they were introduced in Java 8.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The arrow operator (->) is optional in lambda expressions.

A

(False, it is mandatory.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A lambda expression must always have at least one parameter.

A

(False, it can have zero parameters, e.g., () -> true.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The lambda (String x) -> X.startsWith(“test”) is correct.

A

(False, X should be lowercase as x.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The lambda is valid.

(String x, String y) --> x.startsWith("test")
A

It’s valid as long as it matches a functional interface with:
* Two String arguments
* A boolean return type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The lambda x, y -> x.startsWith(“fish”) is valid.

A

(False, parentheses are required for multiple parameters: (x, y) -> x.startsWith(“fish”).)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The lambda x -> { x.startsWith(“camel”); } is valid.

A

(False, missing return statement inside {}.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The lambda x -> { return x.startsWith(“giraffe”) } is valid.

A

(False, missing semicolon inside {}.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

The lambda String x -> x.endsWith(“eagle”) is valid.

A

(False, missing parentheses around x.)

17
Q

The lambda expression s -> {}; is invalid.

A

(False, it is valid as an empty lambda body.)

18
Q

var invalid = (Animal a) -> a.canHop(); compiles successfully.

A

(False, var cannot infer the functional interface type.)

19
Q

Lambdas must be enclosed within a class or method.

A

“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.”

20
Q

A lambda with multiple lines of code does not require braces {}.

A

(False, braces are required for multiple statements.)

21
Q

What is an unbounded generic type in Java?

A

An unbounded generic type ‘<’T’>’ can accept any type, with Object as the implicit upper bound.

22
Q

What is a bounded generic type in Java?

A

A bounded generic type

(<T extends SomeClass>)
restricts T to be a subtype of SomeClass.
23
Q

What does <’T extends Number’> mean?

A

T can be any subclass of Number (e.g., Integer, Double, Float), but not String or other non-numeric types.

24
Q

What does <’T’> mean in Java generics?

A

It means T can be any type, effectively making it equivalent to <’T extends Object’>.

25
What does it enforce? ``` > ```
```It ensures T implements the Comparable interface, allowing objects of T to be compared.```
26
What is '<'T super Integer'>' used for?
It means T can be Integer, Number, or Object—any superclass of Integer.
27
Can an unbounded generic type `````` call methods of Number?
No, because `````` could be any type, not necessarily a Number subclass.
28
What is the benefit of using bounded generics?
It allows type-specific operations (e.g., T extends Number allows T.doubleValue()).
29
What is the difference between <'T'> and <'T' extends 'Object'>?
They are functionally identical, since Object is the default bound for unbounded generics.
30
Which generic declaration allows working with only numeric types?
'<'T' extends Number'>' ensures T is a number type like Integer or Double.
31
Why can a .java file contain a public class?
✔ A .java file can have a public class to make it accessible from anywhere in the project. ✔ The filename must match the public class name.
32
What happens if a .java file has a public class with a different name?
❌ Compilation error! ✔ The filename must match the public class name.
33
What if a .java file has no public class?
✔ The main class has default (package-private) access. ✔ The class is accessible only within the same package. This file can be named anything (e.g., AnyName.java).
34
Can a .java file have multiple classes?
✔ Yes, but only one class can be public. ✔ Other classes must have default (package-private) access. The file must be named MainClass.java.
35
When should a class be public vs. default?
✔ Use public when the class needs to be accessed from other packages. ✔ Use default access when the class is only for internal use within the package. ✅ Helps in encapsulation and better package organization.
36
What makes a lambda expression syntactically incorrect when specifying variable types?
A lambda is incorrect if it specifies variable types without enclosing the parameter list in parentheses. For example, s -> s.length() is correct, but String s -> s.length() is invalid; it should be (String s) -> s.length().
37
How do you correctly declare a type in a lambda expression parameter?
You must wrap the parameter in parentheses, like this: (String s) -> s.length(). Omitting the parentheses when specifying types causes a syntax error.
38
When can you omit parentheses in a lambda expression?
You can omit parentheses only when there's a single untyped parameter, e.g., s -> s.length(). If the parameter has a declared type, parentheses are mandatory: (String s) -> s.length().