🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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"));
    }
}
  1. A.none xy
  2. B.null xy
  3. C.none none
  4. 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 →