Search Tutorials


Java Optional Class MCQ Questions and Answers | JavaInUse

Java Optional Class MCQ Questions and Answers

Q. What is the purpose of the Optional class in Java?

A. To provide a way to represent optional values that may or may not be present
B. To handle null values and prevent null pointer exceptions
C. To create a collection of optional objects
D. To perform functional programming operations

Q. How do you create an Optional object?

A. 'Optional<Type> optional = Optional.of(value);'
B. 'Optional<Type> optional = Optional.empty();'
C. 'Optional<Type> optional = new Optional<>(value);'
D. 'Optional<Type> optional = Optional.fromNullable(value);'

Q. What is the difference between Optional.of and Optional.ofNullable?

A. 'of' creates an 'Optional' with a non-null value, 'ofNullable' creates an 'Optional' with a nullable value
B. 'of' creates an empty 'Optional' if the value is null, 'ofNullable' creates an 'Optional' with the null value
C. 'of' throws an exception if the value is null, 'ofNullable' creates an empty 'Optional'
D. There is no difference, both methods do the same thing

Q. What does the orElse method do?

A. Returns the value if present, otherwise returns the default value
B. Returns the value if present, otherwise throws an exception
C. Returns the value if present, otherwise returns null
D. Returns the value if present, otherwise executes a lambda expression

Q. What is the purpose of the orElseThrow method?

A. To return a default value if the 'Optional' is empty
B. To throw a custom exception if the 'Optional' is empty
C. To return null if the 'Optional' is empty
D. To execute a lambda expression if the 'Optional' is empty

Q. How do you perform a mapping operation on an Optional?

A. 'optional.map(value -> ...)'
B. 'optional.flatMap(value -> ...)'
C. 'optional.filter(value -> ...)'
D. 'optional.orElse(value -> ...)'

Q. What is the difference between map and flatMap?

A. 'map' transforms the value, 'flatMap' transforms the 'Optional' itself
B. 'map' is used for simple transformations, 'flatMap' is used for complex transformations
C. 'map' returns an 'Optional', 'flatMap' returns the transformed value directly
D. There is no difference, both methods do the same thing

Q. How do you filter an Optional?

A. 'optional.filter(condition)'
B. 'optional.map(condition)'
C. 'optional.orElse(condition)'
D. 'optional.ifPresent(condition)'

Q. What does the ifPresent method do?

A. Executes a lambda expression if a value is present
B. Returns a default value if a value is present
C. Throws an exception if a value is present
D. Returns the value if a value is present

Q. How do you get the value from an Optional?

A. 'optional.get()'
B. 'optional.orElse(null)'
C. 'optional.orElseThrow()'
D. 'optional.map(value -> value)'





Q. What is the purpose of the isPresent method?

A. To check if a value is present in the 'Optional'
B. To get the value from the 'Optional'
C. To perform an action if a value is present
D. To throw an exception if a value is not present

Q. How do you handle the case when an Optional is empty?

A. Use 'orElse' to provide a default value
B. Use 'orElseThrow' to throw a custom exception
C. Use 'ifPresent' to handle the empty case
D. Use 'get()' to handle the empty case

Q. Which of the following code snippets demonstrates the correct way to create an Optional object that wraps a non-null value?

A.
Optional<String> optional = Optional.of("Hello");
B.
Optional<String> optional = Optional.ofNullable("Hello");
C.
Optional<String> optional = Optional.empty();
D.
Optional<String> optional = Optional.of(null);

Q. Which of the following code snippets demonstrates the correct way to create an Optional object that wraps a nullable value?

A.
Optional<String> optional = Optional.of("Hello");
B.
Optional<String> optional = Optional.ofNullable("Hello");
C.
Optional<String> optional = Optional.empty();
D.
Optional<String> optional = Optional.of(null);

Q. Which of the following code snippets demonstrates the correct way to create an empty Optional object?

A.
Optional<String> optional = Optional.of("Hello");
B.
Optional<String> optional = Optional.ofNullable("Hello");
C.
Optional<String> optional = Optional.empty();
D.
Optional<String> optional = Optional.of(null);

Q. Which of the following code snippets demonstrates the correct way to check if an Optional object contains a value?

A.
Optional<String> optional = Optional.of("Hello");
if (optional.isPresent()) {
    // Optional contains a value
}
B.
Optional<String> optional = Optional.ofNullable("Hello");
if (!optional.isEmpty()) {
    // Optional contains a value
}
C.
Optional<String> optional = Optional.of("Hello");
if (optional.isNonEmpty()) {
    // Optional contains a value
}
D.
Optional<String> optional = Optional.ofNullable("Hello");
if (optional.hasValue()) {
    // Optional contains a value
}

Q. Which of the following code snippets demonstrates the correct way to get the value from an Optional object, or a default value if the Optional is empty?

A.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElse("World");
B.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseGet(() -> "World");
C.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.get();
D.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow();

Q. Which of the following code snippets demonstrates the correct way to get the value from an Optional object, or throw an exception if the Optional is empty?

A.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElse("World");
B.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseGet(() -> "World");
C.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow();
D.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.get();

Q. Which of the following code snippets demonstrates the correct way to map an Optional object to another Optional using a mapping function?

A.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<Integer> length = optional.map(String::length);
B.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<Integer> length = optional.flatMap(String::length);
C.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<Integer> length = optional.mapToInt(String::length);
D.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<Integer> length = optional.map(s -> s.length());

Q. Which of the following code snippets demonstrates the correct way to filter an Optional object based on a predicate?

A.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<String> filtered = optional.filter(s -> s.length() > 5);
B.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<String> filtered = optional.filter(String::isEmpty);
C.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<String> filtered = optional.filter(s -> !s.isEmpty());
D.
Optional<String> optional = Optional.ofNullable("Hello");
Optional<String> filtered = optional.filter(Predicate.not(String::isEmpty));

Q. Which of the following code snippets demonstrates the correct way to get the value from an Optional object, or a default value calculated using a supplier function if the Optional is empty?

A.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseGet(() -> "World");
B.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElse("World");
C.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow();
D.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.get();

Q. Which of the following code snippets demonstrates the correct way to get the value from an Optional object, or throw a custom exception if the Optional is empty?

A.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow(IllegalArgumentException::new);
B.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow(RuntimeException::new);
C.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow(() -> new RuntimeException());
D.
Optional<String> optional = Optional.ofNullable("Hello");
String value = optional.orElseThrow(() -> new IllegalArgumentException());