← Quiz: Java Optional: all 10 questions
Quiz: Java Optional · Question 10 of 10
A method returns Optional<Student. Which return statement is WRONG when nothing is found?
A method returns Optional<Student>. Which return statement is WRONG when nothing is found?
- A.return Optional.empty();
- B.return Optional.ofNullable(found);
- C.return null;
- D.return Optional.ofNullable(map.get(id));
Answer & explanation
Correct answer: return null;
Returning null from an Optional method defeats its purpose and causes the very NullPointerException it should prevent.
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 →