1Z0-819 Modular Development & Deployment Practices — Questions and Answers
Question 1: What feature was introduced in Java 9 to support modular development?
- Packages
- Modules (Correct answer)
- Interfaces
- Annotations
Correct answer: Modules
Java 9 introduced the Java Platform Module System (JPMS), often referred to as Project Jigsaw, as a major feature to support modular development. This system allows developers to organize code into distinct, self-contained modules, improving encapsulation, reliability, and maintainability of large applications. It also enables the creation of smaller, custom runtime images.
Question 2: Which file defines a module in Java?
- module.xml
- module.config
- module-info.java (Correct answer)
- mod-definition.txt
Correct answer: module-info.java
In the Java Platform Module System, a module is formally defined by a `module-info.java` file. This file, located at the root of the module's source directory, contains declarations such as the module's name, its dependencies on other modules (`requires`), and the packages it makes available to other modules (`exports`). It serves as the module descriptor.
Question 3: What keyword is used to declare a module requirement?
- import
- depends
- requires (Correct answer)
- include
Correct answer: requires
The `requires` keyword is used within a `module-info.java` file to declare a dependency on another module. This explicitly states that the current module needs the functionality (exported packages) provided by the required module to compile and run correctly. This mechanism enforces strong encapsulation and explicit dependencies between modules.
Question 4: What does the 'exports' keyword do in module-info.java?
- Defines class visibility.
- Exposes packages to other modules (Correct answer)
- Initializes class paths.
- Registers libraries.
Correct answer: Exposes packages to other modules
In a `module-info.java` file, the `exports` keyword is used to specify which packages within the current module are accessible to other modules. By default, all packages inside a module are strongly encapsulated and not visible externally. `exports` explicitly opens up selected packages, allowing other modules to use their public types, thereby controlling the module's public API.
Question 5: What is jlink used for?
- To compile Java code.
- To launch web apps.
- To link native libraries.
- To create a custom Java runtime image (Correct answer)
Correct answer: To create a custom Java runtime image
`jlink` is a command-line tool introduced in Java 9 that allows developers to create a custom, minimal Java runtime image. This image contains only the Java modules required by a specific application, significantly reducing the size of the runtime environment. This is particularly beneficial for deploying self-contained applications with a smaller footprint.
Question 6: Which command compiles modules?
- java
- javac --module-source-path (Correct answer)
- jar
- jmod
Correct answer: javac --module-source-path
To compile Java modules, the `javac` command is used, but with the `--module-source-path` option. This option specifies the path where the compiler can find the source code for modules, allowing it to resolve module dependencies and compile the `module-info.java` files along with the regular Java source files. Other options like `java` run code, `jar` packages, and `jmod` creates module archives.
Question 7: What is a named module?
- A default unnamed module.
- A module imported with wildcards.
- A module with a defined name in module-info.java (Correct answer)
- An internal module only.
Correct answer: A module with a defined name in module-info.java
A named module is a module that has a defined name and explicitly declares its dependencies and exported packages in a `module-info.java` file. This contrasts with unnamed modules, which are typically created from classes on the classpath that are not part of a named module. Named modules benefit from the strong encapsulation and explicit dependency management provided by the Java Platform Module System.
Question 8: What tool is used to package compiled modules?
- jmod
- javap
- jar (Correct answer)
- jmap
Correct answer: jar
The `jar` tool is the standard utility in Java for packaging compiled Java classes, resources, and metadata into a single archive file. For modules, `jar` is used to create a modular JAR file, which includes the compiled `module-info.class` file at its root, making it a deployable unit for the Java Platform Module System.
Question 9: What is the benefit of modular programming?
- Increased code duplication.
- Complex dependencies.
- Improved encapsulation and modular deployment (Correct answer)
- Harder to reuse components.
Correct answer: Improved encapsulation and modular deployment
Modular programming, especially with the Java Platform Module System, significantly improves encapsulation by explicitly defining what parts of a module are accessible externally. This leads to more reliable and maintainable code. It also facilitates modular deployment, allowing applications to be built and shipped with only the necessary components, reducing size and complexity.
What feature was introduced in Java 9 to support modular development?