🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← All topics

Quiz: Java Optional: questions with answers

10 questions, each with the answer and a short explanation.

Take the timed quiz + get a certificate →
  1. Q1. What does this print? (code)
  2. Q2. What happens with Optional.of(null)?
  3. Q3. What is the difference between orElse(x()) and orElseGet(() - x())?
  4. Q4. What does this print? (code)
  5. Q5. When should you use flatMap instead of map on an Optional?
  6. Q6. Which is a recommended use of Optional?
  7. Q7. What does this print? (code)
  8. Q8. Which stream operation returns an Optional?
  9. Q9. What does Optional.empty().orElseThrow() do?
  10. Q10. A method returns Optional<Student. Which return statement is WRONG when nothing is found?