PASS - TAKER : Anonymous

Java SE 8 OCP - Lambda Expressions and Functional Interfaces

Correct : 11

78% Complete (success)

78 %

False : 3

21% Complete (success)

21 %

Anonymous 2020-01-08T16:13:52


1. Which of these are functional interfaces? (Choose all that apply.)

For B, we just have one abstract method, so this is a functional interface. Although D includes the equals() method (implying that classes implementing this interface must implement an equals() method), this method doesn’t count because it’s inherited from Object. The default method bloom() is default and so doesn’t count; the method pick() is the functional method.


2. Given the following interface: {code} Which of the following expressions are legal? (Choose all that apply.)

A, B, and E are correct variations on writing lambda expressions. C is invalid syntax for lambda expressions, with a return statement that’s not enclosed in { }. D is incorrect because it has the wrong type for the lambda expression.


3. Which of the following compiles correctly?

The lambda expression is a consumer, so only the type Consumer is correct for this lambda expression—the functional method takes an argument and returns nothing.


4. Given the code fragment: {code} Which method would you use for computeTax() so the code fragment prints Total = 10.50?

computeTax() is a method that takes two arguments, a double and a UnaryOperator, and returns a double value. We know that the lambda expression is a UnaryOperator because the functional method takes a Double and returns a Double (with autoboxing and autounboxing).


5. Given: {code} and the code fragment: List<Reading> readings = Arrays.asList( new Reading(2017, 1, 1, 405.91), new Reading(2017, 1, 8, 405.98), new Reading(2017, 1, 15, 406.14), new Reading(2017, 1, 22, 406.48), new Reading(2017, 1, 29, 406.20), new Reading(2017, 2, 5, 406.03)); Which code fragment will sort the readings in ascending order by value and print the value of each reading?

The sort() method of the List requires a Comparator, which can be expressed as a lambda expression implementing the compare() functional method. This method must return a –1 or 1 (for items that are not equal) depending on the ordering; because we want ascending order, we test if object 1 is less than object 2 and return –1 if so; otherwise, 1 is returned to get ascending order.


6. Given the code fragments: {code} Which code fragment inserted at XXXX will cause a new Human object to be stored in the variable joe?

human is a Supplier, so its functional method is get(). Calling get() returns a new Human with the name Joe and the age 34.


7. Given: {code} and the code fragment: Human jenny = new Human(18, "Jenny"); Human jeff = new Human(17, "jeff"); Human jill = new Human(18, "Jill"); List<Human> people = new ArrayList<>(Arrays.asList(jenny, jeff, jill)); // L1 people.forEach(printAdults); Which code fragment inserted at line // L1 will print the names of only adults (those humans whose age is older than 17)?

We know printAdults must be a Consumer that prints adults in the people ArrayList. The functional method of the Consumer will take a Human object. Then we must test that object to see if the Human is 18 or older, which we can do by defining a Predicate whose functional method returns true if the Human’s age is 18 or older. We use the Predicate by calling the function method, test().


8. Given: {code} What is the result?

The code does not compile because the variable longest from the lambda’s enclosing scope must be final or effectively final, and we are trying to change the value of longest within the lambda.

Not Correct

9. Given the following code fragment: {code} What is the result?

The code does not compile because readingsSupplier is not being assigned a Supplier; rather it is being assigned a List.

Not Correct

10. Given the code fragment: {code} What is the result?

In the last line of the code, we are first calling the apply() method of the foo BiFunction and then calling the apply() method of the bar Function.Looking at the first line of code, we see that foo’s apply() method is implemented by the lambda expression and takes two arguments, an integer n and a String.The method concatenates the String n times (with a space between) and then returns it.

Not Correct

11. Given the code fragment: {code} Which fragment(s), inserted independently at // L1, produce the output? (Choose all that apply.) fir cedar pine

The List replaceAll() method takes a UnaryOperator. In this case, the UnaryOperator’s functional method takes a String and returns the lowercase value of that String. Because it’s a UnaryOperator, we need only specify one type parameter.


12. Given the code fragment: {code} Which fragment(s), inserted independently at // L1, produce the to-do items, both key and value, in the Map? (Choose all that apply.)

To use forEach() with a Map, we need a BiConsumer, whose functional method takes two arguments, both Strings.


13. Given the code fragments: {code} Which fragment(s), inserted independently at // L1, display the output consisting of the to-do item for Tuesday? (Choose all that apply.)

To print the to-do item for Tuesday,we need to call the to-do list’s checkTodoDay(),passing the list of to-dos and a Predicate.We can see in the body of the checkTodoDay() that we call the Predicate’stest() on the map key,which is the day,a String,and if the test passes,we display the value in the map.So Predicate should test to see if the day is equal to "tuesday" and return true if it is.


14. Given the code fragment: {code} Which fragment(s), inserted independently at // L1, produce the output (Choose all that apply.) The answer is : 42.0

To get a double value from a DoubleSupplier, you must use the getAsDouble() functional method.