OCP Java Platform Module System 2 — Questions and Answers
Question 1: Which directive in module-info.java allows a module to expose a package for deep reflection at runtime without exporting it for compile-time access?
- exports
- opens (Correct answer)
- requires
- provides
Correct answer: opens
The `opens` directive allows reflective access to a package at runtime but does not export it for use at compile time.
Question 2: What happens when a module declares `requires transitive com.utils;` and another module depends on that module?
- com.utils is hidden from all downstream modules
- Downstream modules automatically read com.utils without declaring requires (Correct answer)
- com.utils is exported to all unnamed modules
- The JVM throws an error at startup
Correct answer: Downstream modules automatically read com.utils without declaring requires
`requires transitive` implies readability, so any module that reads the declaring module also automatically reads com.utils.
Question 3: Which tool is used to create a custom runtime image that contains only the modules needed by an application?
- javac
- jar
- jlink (Correct answer)
- jmod
Correct answer: jlink
`jlink` assembles a set of modules and their dependencies into a custom, minimal JVM runtime image.
Question 4: A module named `com.app` contains package `com.app.internal`. Which declaration restricts its export to only `com.trusted` module?
- exports com.app.internal;
- exports com.app.internal to com.trusted; (Correct answer)
- opens com.app.internal to com.trusted;
- requires com.trusted;
Correct answer: exports com.app.internal to com.trusted;
The `exports ... to` qualified export restricts which specific modules can access the exported package.
Question 5: Which command-line option allows code in an unnamed module to access a package from a named module that does not export it?
- --module-path
- --add-reads
- --add-exports (Correct answer)
- --patch-module
Correct answer: --add-exports
`--add-exports` overrides module boundaries at runtime or compile time to expose a package that is not otherwise exported.
Question 6: What is the term for a JAR placed on the module path that does not contain a module-info.class?
- Unnamed module
- Automatic module (Correct answer)
- Legacy module
- Classpath module
Correct answer: Automatic module
A JAR on the module path without module-info.class becomes an automatic module, deriving its name from the JAR filename.
Question 7: Which of the following statements about split packages is TRUE in the Java Module System?
- Split packages are allowed across named modules if both export the package
- Split packages are allowed between an automatic module and a named module
- The same package cannot exist in two named modules on the module path (Correct answer)
- Split packages only cause issues at runtime, not at compile time
Correct answer: The same package cannot exist in two named modules on the module path
The JPMS strictly prohibits the same package from being present in two different named modules; this causes a module resolution error.
Which directive in module-info.java allows a module to expose a package for deep reflection at runtime without exporting it for compile-time access?