Java 8 Quiz

Quiz

1. which of the following are not valid lambda expressions?

  1. () -> {}
  2. () -> “Raoul”
  3. () -> {return “Mario”;}
  4. (Integer i) -> return “Alan” + i;
  5. (String s) -> {“Iron Man”;}

Answer

2. Which of these interfaces are functional interfaces?

public interface Adder{
    int add(int a, int b);
}
public interface SmartAdder extends Adder{
    int add(double a, double b);
}
public interface Nothing{
}

Answer

3. Which of the following are valid uses of lambda expressions?

  • A
execute(() -> {});
public void execute(Runnable r){
    r.run();
}
  • B
public Callable<String> fetch() {
    return () -> "Tricky example ;-)";
}
  • C
Predicate<Apple> p = (Apple a) -> a.getWeight();
public interface Predicate<T> {
    boolean predicate(T t);
}

Answer

4. What functional interfaces would you use for the following function descriptors?

  1. T -> R
  2. (int, int) -> int
  3. T -> void
  4. () -> T
  5. (T, U) -> R

Answer

5. How could you fix the problem?

Object o = () -> {System.out.println(“Tricky example”); };

Answer

6. What are equivalent method references for the following lambda expressions?

  • A

Function<String, Integer> stringToInteger = (String s) -> Integer.parseInt(s);

  • B

BiPredicate<List, String> contains = (list, element) -> list.contains(element);

Answer

Answer

1. Only 4 and 5 are invalid lambdas

To correct 4:

(Integer i) -> {return "Alan" + i;}

To correct 5:

(String s) -> {return "Iron Man";}

2. Only Adder is a functional interface

3. Only A and B are valid.

A is valid because the lambda () -> {} has the signature () -> void, which matches the signature of the abstract method run defined in Runnable. Note that running this code will do nothing because the body of the lambda is empty!

C is invalid because the lambda expression (Apple a) -> a.getWeight() has the signature (Apple) -> Integer, which is different than the signature of the method test defined in Predicate: (Apple) -> boolean.

4. all

  1. Function<T, R> is a good candidate. It’s typically used for converting an object of type T into an object of type R (for example, Function<Apple, Integer> to extract the weight of an apple).
  2. IntBinaryOperator has a single abstract method called applyAsInt representing a function descriptor (int, int) -> int.
  3. Consumer has a single abstract method called accept representing a function descriptor T -> void.
  4. Supplier has a single abstract method called get representing a function descriptor () -> T. Alternatively, Callable also has a single abstract method called call representing a function descriptor () -> T.
  5. BiFunction<T, U, R> has a single abstract method called apply representing a function descriptor (T, U) -> R.

5. answer

The context of the lambda expression is Object (the target type). But Object isn’t a functional interface. To fix this you can change the target type to Runnable, which represents a function descriptor () -> void:

Runnable r = () -> {System.out.println(“Tricky example”); };

6. answer for 6

  • A

Function<String, Integer> stringToInteger = Integer::parseInt

  • B

BiPredicate<List, String> contains = List::contains;