1Z0-830 practice question 1 of 25

A consumer needs to access a service provided by another module. Which of the following statements is TRUE?

Full set: 116 questions
Answer options
A:

A. The consumer module automatically has access to all services from other modules.

B:

B. The service module needs to expose the service interface using opens in module-info.java.

Correct answer
C:

C. Reflection can be used to access the service implementation directly, bypassing providers.

D:

D. Consumers directly interact with provider implementations, not the service interface.

Correct Answer:
B. B. The service module needs to expose the service interface using opens in module-info.java.
Explanation

This question is flawed, but option B is the most plausible intended answer. For a consumer to use a service, the module that defines the service interface must make that interface accessible. It does this by using the exports directive for the package containing the interface. The provider module then uses provides...with and the consumer module uses uses.
Option B incorrectly states the keyword is opens (which is for deep reflection) instead of exports. However, the other options are conceptually incorrect about the service locator pattern, whereas B correctly identifies the need for the service's module to explicitly expose the interface.

Why Incorrect

A. Access is not automatic. The consumer module must explicitly declare its intent to use the service with the uses directive in its module-info.java.
C. The service locator pattern is designed to prevent this. Bypassing the provider mechanism via reflection violates the encapsulation and decoupling principles of the module system.
D. Consumers interact with the service through its public interface, not the concrete implementation. The ServiceLoader API provides instances of the implementation, cast to the interface type.

References

1. Oracle Corporation. (2023). Java® SE 21 & JDK 21: ServiceLoader Class Documentation. Java Platform, Standard Edition 21 API Specification. Retrieved from https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/ServiceLoader.html. (The class documentation describes the roles of service, provider, and consumer.)

2. Oracle Corporation. (2023). The Java™ Tutorials: The Module System. Services with the Module System. Retrieved from https://docs.oracle.com/javase/tutorial/modules/services.html. (This tutorial shows the module defining the service interface must export its package.)

Scroll to Top