← 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())?
- A.There is no difference
- B.orElseGet always calls x(), orElse never does
- C.orElse always evaluates x(); orElseGet calls x() only when the Optional is empty
- 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 →