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

What does Optional.empty().orElseThrow() do?

What does Optional.empty().orElseThrow() do?

  1. A.Returns null
  2. B.Returns an empty string
  3. C.Throws NoSuchElementException
  4. D.Throws NullPointerException
Answer & explanation

Correct answer: Throws NoSuchElementException

With no argument, orElseThrow throws NoSuchElementException when the Optional is empty.

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 →