Chapter 8 Lambdas and Functional Interfaces Flashcards
Review Questions
What is the result of the following class?
1: import java.util.function.*;
2:
3: public class Panda {
4: int age;
5: public static void main(String[] args) {
6: Panda p1 = new Panda();
7: p1.age = 1;
8: check(p1, p -> p.age < 5);
9: }
10: private static void check(Panda panda,
11: Predicate<Panda> pred) {
12: String result =
13: pred.test(panda) ? "match" : "not match";
14: System.out.print(result);
15: } }
1.
A. match
B. not match
C. Compiler error on line 8
D. Compiler error on lines 10 and 11
E. Compiler error on lines 12 and 13
F. A runtime exception is thrown.</Panda>
What is the result of the following code?
1: interface Climb {
2: boolean isTooHigh(int height, int limit);
3: }
4:
5: public class Climber {
6: public static void main(String[] args) {
7: check((h, m) -> h.append(m).isEmpty(), 5);
8: }
9: private static void check(Climb climb, int height) {
10: if (climb.isTooHigh(height, 10))
11: System.out.println(“too high”);
12: else
13: System.out.println(“ok”);
14: }
15: }
2.
A. ok
B. too high
C. Compiler error on line 7
D. Compiler error on line 10
E. Compiler error on a different line
F. A runtime exception is thrown.
- Which statements about functional interfaces are true? (Choose all that apply.)
A. A functional interface can contain default and private methods.
B. A functional interface can be defined as a class or an interface.
C. Abstract methods with signatures that are contained in public methods
of java.lang.Object do not count toward the abstract method count
for a functional interface.
D. A functional interface cannot contain static or private static
methods.
E. A functional interface must be marked with the
@FunctionalInterface annotation.
Which lambda can replace the MySecret class to return the same value? (Choose all that
apply.)
interface Secret {
String magic(double d);
}
class MySecret implements Secret {
public String magic(double d) {
return “Poof”;
} }
4.
A. (e) -> “Poof”
B. (e) -> {“Poof”}
C. (e) -> { String e = “”; “Poof” }
D. (e) -> { String e = “”; return “Poof”; }
E. (e) -> { String e = “”; return “Poof” }
F. (e) -> { String f = “”; return “Poof”; }
- Which of the following functional interfaces contain an abstract method that returns a
primitive value? (Choose all that apply.)
A. BooleanSupplier
B. CharSupplier
C. DoubleSupplier
D. FloatSupplier
E. IntSupplier
F. StringSupplier
- Which of the following lambda expressions can be passed to a function of
Predicate<String> type? (Choose all that apply.)
A. s -> s.isEmpty()
B. s --> s.isEmpty()
C. (String s) -> s.isEmpty()
D. (String s) --> s.isEmpty()
E. (StringBuilder s) -> s.isEmpty()
F. (StringBuilder s) --> s.isEmpty()</String>
Which of these statements is true about the following code?
public void method() {
x((var x) -> {}, (var x, var y) -> false);
}
public void x(Consumer<String> x, BinaryOperator<Boolean> y) {}
7.
A. The code does not compile because of one of the variables named x.
B. The code does not compile because of one of the variables named y.
C. The code does not compile for another reason.
D. The code compiles, and the x in each lambda refers to the same type.
E. The code compiles, and the x in each lambda refers to a different type.</Boolean></String>
Which of the following is equivalent to this code? (Choose all that apply.)
UnaryOperator<Integer> u = x -> x * x;
8.
A. BiFunction<Integer> f = x -> x*x;
B. BiFunction<Integer, Integer> f = x -> x*x;
C. BinaryOperator<Integer, Integer> f = x -> x*x;
D. Function<Integer> f = x -> x*x;
E. Function<Integer, Integer> f = x -> x*x;
F. None of the above</Integer></Integer></Integer>
- Which statements are true? (Choose all that apply.)
A. The Consumer interface is good for printing out an existing value.
B. The Supplier interface is good for printing out an existing value.
C. The IntegerSupplier interface returns an int.
D. The Predicate interface returns an int.
E. The Function interface has a method named test().
F. The Predicate interface has a method named test().
Which of the following can be inserted without causing a compilation error? (Choose all that
apply.)
public void remove(List<Character> chars) {
char end = 'z';
Predicate<Character> predicate = c -> {
char start = 'a'; return start <= c && c <= end; };
// INSERT LINE HERE
}
10.
A. char start = 'a';
B. char c = 'x';
C. chars = null;
D. end = '1';
E. None of the above</Character></Character>
How many times istrue printed out by this code?
import java.util.function.Predicate;
public class Fantasy {
public static void scary(String animal) {
var dino = s -> “dino”.equals(animal);
var dragon = s -> “dragon”.equals(animal);
var combined = dino.or(dragon);
System.out.println(combined.test(animal));
}
public static void main(String[] args) {
scary(“dino”);
scary(“dragon”);
scary(“unicorn”);
}
}
11.
A. One
B. Two
C. Three
D. The code does not compile.
E. A runtime exception is thrown.
What does the following code output?
Function<Integer, Integer> s = a -> a + 4;
Function<Integer, Integer> t = a -> a * 3;
Function<Integer, Integer> c = s.compose(t);
System.out.print(c.apply(1));
12.
A. 7
B. 15
C. The code does not compile because of the data types in the lambda
expressions.
D. The code does not compile because of thecompose() call.
E. The code does not compile for another reason.
Which is true of the following code?
int length = 3;
for (int i = 0; i<3; i++) {
if (i%2 == 0) {
Supplier<Integer> supplier = () -> length; // A
System.out.println(supplier.get()); // B
} else {
int j = i;
Supplier<Integer> supplier = () -> j; // C
System.out.println(supplier.get()); // D
}
}
13.
A. The first compiler error is on lineA.
B. The first compiler error is on lineB.
C. The first compiler error is on lineC.
D. The first compiler error is on lineD.
E. The code compiles successfully.</Integer></Integer>
- Which of the following are valid lambda expressions? (Choose all that apply.)
A. (Wolf w, var c) -> 39
B. (final Camel c) -> {}
C. (a,b,c) -> {int b = 3; return 2;}
D. (x,y) -> new RuntimeException()
E. (var y) -> return 0;
F. () -> {float r}
G. (Cat a, b) -> {}
Which lambda expression, when entered into the blank line in the following code, causes the
program to printhahaha? (Choose all that apply.)
import java.util.function.Predicate;
public class Hyena {
private int age = 1;
public static void main(String[] args) {
var p = new Hyena();
double height = 10;
int age = 1;
testLaugh(p, _________________________);
age = 2;
}
static void testLaugh(Hyena panda, Predicate<Hyena> joke) {
var r = joke.test(panda) ? "hahaha" : "silence";
System.out.print(r);
}
}
15.
A. var -> p.age <= 10
B. shenzi -> age==1
C. p -> true
D. age==1
E. shenzi -> age==2
F. h -> h.age < 5
G. None of the above, as the code does not compile</Hyena>