Chapter 12: Modules Flashcards

1
Q

What is the main purpose of the Java Platform Module System (JPMS)?

A

JPMS groups related packages into modules, allowing controlled access and improved code organization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does JPMS compare to JAR files?

A

Like a JAR file, but with explicit control over which packages are accessible outside the module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three key components of JPMS?

A

Module JAR format, JDK partitioning into modules, and additional Java tool command-line options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a module in Java?

A

A group of one or more packages plus a module-info.java file that declares module properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why were Java modules introduced? 5 items

A

To improve access control, dependency management, security, performance, and to support custom Java builds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do modules provide better access control?

A

They allow packages to be accessible only within the module, preventing unintended access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do modules improve dependency management?

A

They declare dependencies, enabling Java to detect missing JARs at startup instead of runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a key advantage of using custom Java builds?

A

They include only necessary JDK components, reducing runtime size and improving efficiency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do modules enhance security?

A

By omitting unused JDK parts, reducing exposure to vulnerabilities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do modules improve performance?

A

Smaller runtime size leads to faster startup and lower memory usage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is unique package enforcement in Java modules?

A

Ensures each package comes from only one module, preventing conflicts and confusion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do Java modules enforce strong encapsulation?

A

Packages are not accessible by default; only explicitly exported packages can be used by other modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What keyword is used to expose a package in Java modules?

A

The exports keyword, e.g., exports com.example.api;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can a module restrict access to its exported packages?

A

By using exports … to, e.g., exports com.example.api to com.example.moduleB; to limit access to a specific module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can a module allow reflective access to a package?

A

Using the opens keyword, e.g., opens com.example.internal to some.framework.module;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the requires keyword do in a Java module?

A

It specifies dependencies on other modules, ensuring only required modules are accessible.

17
Q

What does the –module-path option do in Java’s module system?

A

The –module-path option specifies the locations of required modules. It allows the Java runtime to locate and load modules that are needed for execution. Multiple module paths can be separated using ; on Windows or : on macOS/Linux.

18
Q

How do you execute a specific class from a named module using the –module option?

A

The –module option is used to specify a module and the class within it that should be executed.

19
Q

What is a named module in Java?

A

A named module is a module that has an explicit name defined in a module-info.java file. It is part of Java’s module system and follows strict encapsulation rules. Named modules explicitly declare their dependencies and exported packages.

20
Q

How is a named module different from an unnamed module?

A

A named module has a module-info.java file and follows strict module rules, whereas an unnamed module does not have a module descriptor and can access all other modules. Named modules provide better encapsulation and dependency management.

21
Q

What is the purpose of the jdeps command in Java?

A

jdeps analyzes the dependencies of Java class files and shows the packages or classes that are required by a given class or JAR file.

22
Q

How do you use jdeps to analyze a JAR file and display package-level dependencies?

A

You run jdeps myapp.jar, and it will show which packages the classes in myapp.jar depend on.

23
Q

Which jdeps option shows class-level dependencies instead of package-level?

A

The -verbose:class option displays class-level dependencies: jdeps -verbose:class myapp.jar.

24
Q

What does the

--module-path a.jar:b.jar
option do in a jdeps command?
A

It tells jdeps where to find required modules, treating both a.jar and b.jar as modular JARs. This enables jdeps to resolve dependencies correctly.

25
Q

What is the purpose of

--module com.example.a
in jdeps?
A

It specifies the module to analyze, using the name defined in module-info.java (in this case, com.example.a).

26
Q

Why is jdeps –module-path … –module … preferred over –class-path for modular JARs?

A

Because –module-path respects JPMS (Java Platform Module System) and understands named and automatic modules, while –class-path treats JARs as legacy classpath entries.

27
Q

Why does a Java modular project require a folder structure like src/com.example/com/example/Main.java?

A

In a Java modular project using the –module-source-path option, the compiler expects the source root (e.g., src/) to contain one folder per module, where the folder name matches the module name declared in module-info.java. Inside that folder, the standard Java package structure begins. So for a module named com.example, the correct structure is src/com.example/com/example/Main.java for a class in the com.example package.

28
Q

What does the –module-source-path option tell the Java compiler about source structure?

A

The –module-source-path option tells the Java compiler that each immediate subdirectory of the given source root (e.g., src/) is a distinct module. The name of each folder must match the module declaration in its module-info.java, and inside that folder, source files must follow the standard package-based directory structure (e.g., com/example for package com.example;).

29
Q

What happens if the folder structure in a modular Java project does not separate the module folder from the package folders?

A

If the source files in a modular Java project are placed directly under the package structure (e.g., src/com/example) instead of under a module-named folder (e.g., src/com.example/com/example), the compiler will not recognize the files as belonging to a named module. This results in errors like “module not found on module source path,” because the expected module-info.java and the corresponding package files aren’t located under a properly named module directory.