1Z0-819 Modular Development & Deployment Practices 4 — Questions and Answers
Question 1: What is the correct directory layout expected when placing a module named `com.example` on the module path?
- The JAR or directory must be directly on the module path; no specific subdirectory is required
- A subdirectory named `com/example/` containing `module-info.class` at its root
- A subdirectory named `com.example/` containing `module-info.class` at its root (Correct answer)
- A flat directory with all `.class` files including `module-info.class`
Correct answer: A subdirectory named `com.example/` containing `module-info.class` at its root
When using an exploded module directory, the module path entry should be the parent directory, and the module root must be a directory named after the module (e.g., `com.example/`) containing `module-info.class`.
Question 2: Which option to `jlink` sets the name and location of the output runtime image?
- --destination
- --output (Correct answer)
- --image-dir
- --target
Correct answer: --output
`jlink --output <path>` specifies the directory where the generated runtime image will be written.
Question 3: A `ServiceLoader` call returns no providers even though a provider JAR is on the module path. What is the most likely cause?
- The provider module is missing a `uses` directive
- The provider module does not declare `provides <service> with <impl>` in its module-info.java (Correct answer)
- The consumer module does not export the service interface package
- ServiceLoader is not supported with the module system
Correct answer: The provider module does not declare `provides <service> with <impl>` in its module-info.java
A provider module must declare `provides <ServiceInterface> with <ImplementationClass>` in its `module-info.java` to be discovered by `ServiceLoader`.
Question 4: What does the `--patch-module` JVM flag do?
- Replaces a module entirely with a different JAR at runtime
- Adds classes or resources into an existing named module at runtime (Correct answer)
- Downgrades a named module to an automatic module
- Enables reflection on all packages in the specified module
Correct answer: Adds classes or resources into an existing named module at runtime
`--patch-module <module>=<path>` merges additional classes or resources from the given path into the named module, commonly used for testing.
Question 5: Which statement about `module-info.java` and multi-release JARs is correct?
- Each versioned directory in a multi-release JAR must have its own module-info.java
- A single module-info.java at the root applies to all Java versions in the JAR (Correct answer)
- Multi-release JARs cannot be placed on the module path
- module-info.java must be placed in the META-INF/versions/11/ directory for Java 11
Correct answer: A single module-info.java at the root applies to all Java versions in the JAR
A multi-release JAR has one `module-info.java` at the root (or in the base entry) that defines the module for all versions contained in the JAR.
Question 6: What is the effect of compiling a module with `javac --release 11 --module-source-path src -m com.example`?
- Compiles only the module-info.java file of com.example
- Compiles all modules found under the source path, ignoring the -m flag
- Compiles the com.example module and its sources from the directory matching its name under src/ (Correct answer)
- Compiles com.example and automatically downloads its dependencies
Correct answer: Compiles the com.example module and its sources from the directory matching its name under src/
`-m com.example` with `--module-source-path` tells `javac` to compile the named module, looking for its sources in the `src/com.example/` subtree.
Question 7: What is the role of `module-info.java`'s `provides` directive in conjunction with `ServiceLoader`?
- It declares which packages the module exports to service consumers
- It registers a concrete implementation class as a provider for a given service interface (Correct answer)
- It specifies which module is allowed to call ServiceLoader.load() for this service
- It marks the service interface as part of this module's public API
Correct answer: It registers a concrete implementation class as a provider for a given service interface
`provides <Interface> with <Implementation>` registers the implementation as a service provider discoverable via `ServiceLoader` without needing META-INF/services files.
What is the correct directory layout expected when placing a module named `com.example` on the module path?