← Quiz: Java Optional: all 10 questions
Quiz: Java Optional · Question 4 of 10
What does this print? (code)
What does this print?
import java.util.*;
public class Main {
public static void main(String[] args) {
Optional<Integer> m = Optional.of(35);
System.out.println(m.filter(x -> x >= 40).map(x -> "pass").orElse("fail"));
}
}
- A.pass
- B.fail
- C.35
- D.Optional.empty
Answer & explanation
Correct answer: fail
35 fails the filter, so the Optional becomes empty and orElse gives "fail".
Can you get the rest right without peeking?
Take the timed Quiz: Java Optional quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →