OCP Java Platform Module System 3 — Questions and Answers
Question 1: In a `module-info.java`, which directive is used to declare that a module consumes a service defined by an interface?
- provides
- uses (Correct answer)
- requires
- opens
Correct answer: uses
The `uses` directive declares that the module is a consumer of a service interface via ServiceLoader.
Question 2: What does the `--limit-modules` command-line option do during module resolution?
- Sets a maximum number of modules that can be loaded
- Restricts the universe of observable modules to a given set (Correct answer)
- Prevents any unnamed module from being loaded
- Disables automatic module name derivation
Correct answer: Restricts the universe of observable modules to a given set
`--limit-modules` restricts the observable module universe to the specified modules and their transitive dependencies.
Question 3: Which of the following correctly declares a service provider in module-info.java for a service interface `com.api.Processor` with implementation `com.impl.FastProcessor`?
- uses com.api.Processor with com.impl.FastProcessor;
- provides com.api.Processor with com.impl.FastProcessor; (Correct answer)
- exports com.impl.FastProcessor to com.api.Processor;
- requires com.api.Processor provides com.impl.FastProcessor;
Correct answer: provides com.api.Processor with com.impl.FastProcessor;
The `provides ... with` directive registers a concrete implementation class for a service interface in the module system.
Question 4: Which module contains the `java.lang` package in a standard JDK modular installation?
- java.base (Correct answer)
- java.se
- java.lang
- java.core
Correct answer: java.base
`java.base` is the foundational module that contains core packages like java.lang, java.util, and java.io.
Question 5: What is the name derived for an automatic module created from a JAR file named `my-library-2.1.0.jar` when no Automatic-Module-Name manifest entry is present?
- my-library-2.1.0
- my.library (Correct answer)
- mylibrary
- my-library
Correct answer: my.library
The JVM strips version numbers and converts hyphens to dots, deriving `my.library` as the automatic module name.
Question 6: Which option in the `jar` command is used to specify the main class when creating a modular JAR?
- --main-class (Correct answer)
- --entry-point
- --manifest
- --module-main
Correct answer: --main-class
The `--main-class` option in the `jar` command records the application entry point in the JAR manifest's Main-Class attribute.
Question 7: A module `com.app` opens package `com.app.data` to `com.framework`. Which statement about this is TRUE?
- com.framework can use types from com.app.data at compile time
- com.framework can only use public members of com.app.data
- com.framework can reflectively access all members, including private ones, in com.app.data (Correct answer)
- All modules can reflectively access com.app.data
Correct answer: com.framework can reflectively access all members, including private ones, in com.app.data
A qualified `opens ... to` directive grants the specified module deep reflective access, including private members, to that package.
In a `module-info.java`, which directive is used to declare that a module consumes a service defined by an interface?