🎙 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 3 of 10

What is the difference between orElse(x()) and orElseGet(() - x())?

What is the difference between orElse(x()) and orElseGet(() -> x())?

  1. A.There is no difference
  2. B.orElseGet always calls x(), orElse never does
  3. C.orElse always evaluates x(); orElseGet calls x() only when the Optional is empty
  4. D.orElse throws when empty
Answer & explanation

Correct answer: orElse always evaluates x(); orElseGet calls x() only when the Optional is empty

The argument of orElse is computed before the method runs; the supplier of orElseGet is called lazily.

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 →