Modules and imports: Java 25 practice questions

Modules and imports is 6% of the free Java 25 mock (3 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.

J.VIII · Java 25 practice question

A developer adds `import module com.example.core;` to a compilation unit. Which types from `com.example.core` become available on demand?

Choose one answer
Check answer and explanation

Correct answer: All public top-level classes and interfaces in packages exported by com.example.core to the current module

Explanation

A single-module-import declaration brings in only the public top-level classes and interfaces from packages that the named module exports to the current module.

Common trap

Package-private types are never accessible across module boundaries, so a module import does not expose them. Module encapsulation still applies; packages that are not exported to the current module are not imported.

Source: JLS SE 25 §7.5.5. Single-Module-Import Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

When a compilation unit includes a single-type-import declaration `import TypeName;`, where does the imported type become available under its simple name?

Choose one answer
Check answer and explanation

Correct answer: In the module, class, and interface declarations of that compilation unit

Explanation

A single-type-import declaration makes the imported class or interface available under its simple name throughout the module, class, and interface declarations of the compilation unit where it appears.

Common trap

Module declarations (such as module-info.java) can also use single-type-import declarations, for example to shorten service type names in uses or provides directives. Import declarations are scoped strictly to the compilation unit in which they appear, not to the entire package.

Source: JLS SE 25 §7.5.1. Single-Type-Import Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

A team exports `com.acme.model` from their module without adding an `opens` directive. Can another module use reflection to access public types and members in `com.acme.model`?

Choose one answer
Check answer and explanation

Correct answer: Yes, exporting a package also grants reflective access to its public and protected types and members

Explanation

An exports directive grants reflective access to the exported package's public and protected types and members in addition to compile-time and run-time access.

Common trap

Developers often conflate deep reflection on private members (which requires opens) with all reflective access, forgetting that exports already permits reflection on public and protected elements. Reflective access to exported public and protected types is granted to code in other modules in general, not restricted to unnamed modules.

Source: JLS SE 25 §7.7.2. Exported and Opened Packages
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

What happens if a `requires` directive is placed inside the module declaration of `java.base`?

Choose one answer
Check answer and explanation

Correct answer: A compile-time error occurs because java.base is the primordial module and has no dependences

Explanation

Because java.base is the primordial module with no dependences, placing any requires directive in its module declaration results in a compile-time error.

Common trap

Even without a cycle, java.base is prohibited from declaring any dependences at all. This is a hard compile-time error in the specification, not a warning that the compiler ignores.

Source: JLS SE 25 §7.7.1. Dependences
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

A library module declares `exports com.acme.api;` in its module declaration. What compile-time and run-time access does this grant to code in other modules?

Choose one answer
Check answer and explanation

Correct answer: Access to public and protected types in com.acme.api, along with their public and protected members

Explanation

Exporting a package grants other modules compile-time and run-time access to both public and protected types in that package, as well as the public and protected members of those types.

Common trap

It is easy to forget that protected types and protected members are also accessible (for example, to subclasses in other modules) when a package is exported. Exporting a package never bypasses standard access control for package-private or private declarations.

Source: JLS SE 25 §7.7.2. Exported and Opened Packages
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

In a module declaration, what is the purpose of a `provides ... with ...` directive?

Choose one answer
Check answer and explanation

Correct answer: It specifies a service and one or more service providers for java.util.ServiceLoader

Explanation

The provides directive pairs a service type with one or more service provider implementations via its with clause for discovery by java.util.ServiceLoader.

Common trap

Qualified exports use `exports ... to ...`, not `provides ... with ...`. The Java module system does not perform annotation-based dependency injection; it integrates with java.util.ServiceLoader.

Source: JLS SE 25 §7.7.4. Service Provision
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

A module wants to provide an implementation for a service interface declared in a different module. Under what condition does this compile?

Choose one answer
Check answer and explanation

Correct answer: It compiles as long as the service is accessible to code in the current module

Explanation

A provides directive may reference a service declared in another module, provided that the service is accessible to code in the current module; otherwise, a compile-time error occurs.

Common trap

Service providers are commonly split into separate modules from the service API, so cross-module service provision is fully supported. Any accessible module can declare the service, not just java.base.

Source: JLS SE 25 §7.7.4. Service Provision
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

In a normal (non-open) module, which packages are accessible via the Core Reflection API from other modules?

Choose one answer
Check answer and explanation

Correct answer: Only packages that are explicitly exported, explicitly opened, or both

Explanation

A normal module permits reflective access only to types in packages that are explicitly exported, explicitly opened, or both.

Common trap

This wrongly excludes exported packages, which do grant reflective access to their public and protected types and members. Having a public class in a package does not grant reflective access unless the package is explicitly exported or opened.

Source: JLS SE 25 §7.7. Module Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

How does an `open module` differ from a normal module with respect to the Core Reflection API?

Choose one answer
Check answer and explanation

Correct answer: An open module grants reflective access to types in all of its packages, as if every package were opened

Explanation

An open module grants reflective access to types across all of its packages as though every package had been explicitly opened, whereas a normal module restricts reflective access to explicitly exported or opened packages.

Common trap

Marking a module open affects reflective access, not compile-time accessibility; packages still need exports directives to be accessible at compile time. Restricting reflection to explicitly exported or opened packages is the behavior of a normal module, not an open module.

Source: JLS SE 25 §7.7. Module Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

Which two responsibilities does a named module's declaration fulfill for controlling the universe of available types?

Choose one answer
Check answer and explanation

Correct answer: It specifies dependences on other modules and specifies which of its own packages are exported or opened

Explanation

A module declaration defines a named module by declaring dependences on other modules for its own code and specifying which of its packages are exported or opened for other modules that depend on it.

Common trap

Module declarations operate at the package and module level, not by enumerating individual class files or per-class versions. JVM memory settings and ahead-of-time compilation are runtime or build concerns, not part of a JLS module declaration.

Source: JLS SE 25 §7.7. Module Declarations
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

A developer writes `module com.example.app {}` without any `requires` directives. Can code in `com.example.app` still use types from `java.base`?

Choose one answer
Check answer and explanation

Correct answer: Yes, every module other than java.base has an implicitly declared dependence on java.base if not stated

Explanation

Whenever a module declaration other than java.base does not explicitly require java.base, it automatically has an implicitly declared dependence on java.base.

Common trap

While other JDK modules like java.sql must be required explicitly, java.base is a special case that is always implicitly required. No compiler flag is needed; the implicit dependence on java.base is mandated directly by the Java Language Specification.

Source: JLS SE 25 §7.7.1. Dependences
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


J.VIII · Java 25 practice question

A developer writes `provides com.acme.LogLevel with com.acme.CustomLevel;` in a module declaration, where `LogLevel` is an `enum` class. What is the result?

Choose one answer
Check answer and explanation

Correct answer: A compile-time error occurs because a provides directive cannot specify an enum class as the service

Explanation

Specifying an enum class as the service in a provides directive is explicitly a compile-time error.

Common trap

Enum classes cannot be subclassed in normal code anyway, and the module system explicitly rejects enum classes as services at compile time. The error is caught during compilation rather than deferred to ServiceLoader at run time.

Source: JLS SE 25 §7.7.4. Service Provision
Checked against the cited text on 2026-11-25.

Report an error in this question

Open this question on its own page


Back to all topics