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

What happens with Optional.of(null)?

What happens with Optional.of(null)?

  1. A.It returns an empty Optional
  2. B.It throws NullPointerException
  3. C.It returns Optional[null]
  4. D.It does not compile
Answer & explanation

Correct answer: It throws NullPointerException

of requires a non-null value. Use ofNullable when the value might be null.

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 →