Return to site

JAVA CERTIFICATION QUESTION: Use the Optional class

· java

The goal of this quiz is to test your knowledge of the Java Optional class.

Given the class:

import java.util.Optional;
public class OptionalArgs {
  public static void main(String[] args) {
    var v = Optional.ofNullable(args[0])
             .or(() -> Optional.ofNullable(args[1]))
             .flatMap(o -> Optional.empty())
             .stream().findFirst().orElse("default");
    System.out.print(v);
  }
}

What is the output if you run the compiled class as shown below? Choose one.

java OptionalArgs Java
  • A. A runtime exception
  • B. Blank output
  • C. Java
  • D. default

 

The answer is D.