← Quiz: Java 8 Lambdas and Streams: all 10 questions
Quiz: Java 8 Lambdas and Streams · Question 7 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
Optional<String> empty = Optional.empty();
Optional<String> x = Optional.of("x");
System.out.println(empty.orElse("none") + " " + x.map(s -> s + "y").orElse("none"));
}
}
- A.none xy
- B.null xy
- C.none none
- D.none x
Answer & explanation
Correct answer: none xy
orElse gives the fallback for an empty Optional; map transforms the value only when one is present.
Can you get the rest right without peeking?
Take the timed Quiz: Java 8 Lambdas and Streams quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →