🎙 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 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?

  1. A.return Optional.empty();
  2. B.return Optional.ofNullable(found);
  3. C.return null;
  4. 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 →