ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
System.out.print(Optional.ofNullable(null).map(Object::toString).orElse("none") + " ");
System.out.print(Optional.of(null).isPresent());
  1. A. none false
  2. B. "none " is printed, then a NullPointerException is thrown
  3. C. null false
  4. D. A NullPointerException is thrown before anything is printed

Correct answer: B. "none " is printed, then a NullPointerException is thrown

ofNullable(null) gives an empty Optional, map is skipped and orElse supplies "none". Optional.of requires a non-null value and throws NullPointerException immediately.

More OOP with C++ and Java questions

Exams that ask this topic