← 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)?
- A.It returns an empty Optional
- B.It throws NullPointerException
- C.It returns Optional[null]
- 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 →