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