1. What is the purpose of the Optional class in Java?
a) To handle null values more gracefully
b) To create immutable objects
c) To manage thread synchronization
d) To optimize memory usage
Answer:
a) To handle null values more gracefully
Explanation:
The Optional class in Java is used to represent optional values that are either present or absent, helping to avoid null pointer exceptions.
2. How do you create an empty Optional object?
a) Optional.empty()
b) new Optional()
c) Optional.of(null)
d) Optional.none()
Answer:
a) Optional.empty()
Explanation:
Optional.empty() is used to create an empty Optional object.
3. What does Optional.ofNullable() do?
a) Creates an Optional object with a non-null value
b) Creates an Optional object that may or may not contain a non-null value
c) Always creates an empty Optional object
d) Throws an exception if the value is null
Answer:
b) Creates an Optional object that may or may not contain a non-null value
Explanation:
Optional.ofNullable() is used to create an Optional object that can hold a null value.
4. How do you retrieve the value from an Optional object?
a) get()
b) getValue()
c) retrieve()
d) obtain()
Answer:
a) get()
Explanation:
The get() method is used to retrieve the value from an Optional object.
5. How do you check if an Optional object contains a value?
a) isEmpty()
b) isPresent()
c) hasValue()
d) contains()
Answer:
b) isPresent()
Explanation:
The isPresent() method checks if the Optional object contains a non-null value.
6. What is the purpose of the orElse() method in Optional?
a) To throw an exception if value is not present
b) To provide a default value if the Optional is empty
c) To filter the value in the Optional
d) To transform the value in the Optional
Answer:
b) To provide a default value if the Optional is empty
Explanation:
orElse() returns the value if present; otherwise, it returns a default value provided.
7. What happens when you call get() on an empty Optional object?
a) Returns null
b) Returns a default value
c) An NoSuchElementException is thrown
d) An OptionalException is thrown
Answer:
c) An NoSuchElementException is thrown
Explanation:
Calling get() on an empty Optional object results in a NoSuchElementException.
8. Which method in Optional can be used to execute a block of code if a value is present?
a) ifPresent()
b) ifAvailable()
c) execute()
d) run()
Answer:
a) ifPresent()
Explanation:
ifPresent() is used to execute a block of code if the Optional object contains a non-null value.
9. How do you transform the value in an Optional object using a function?
a) map()
b) transform()
c) apply()
d) change()
Answer:
a) map()
Explanation:
The map() function applies a function to the value in the Optional object if it is present.
10. What is the difference between the map() and flatMap() methods in Optional?
a) map() can return null, flatMap() cannot
b) map() returns an Optional, flatMap() returns the value directly
c) flatMap() flattens nested Optionals, map() does not
d) There is no difference
Answer:
c) flatMap() flattens nested Optionals, map() does not
Explanation:
flatMap() is used for mapping functions that return Optional, effectively preventing nested Optionals.