Chapter 10: Modules Flashcards

1
Q

What is a module?

A

A module is a group of one or more packages plus a special file called module-info.java

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

Given a class in a package ‘zoo.animal.feeding’, create a module-info.java file with no body for that module.

A

file contents:

module zoo.animal.feeding { }

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

Where must the file module-info.java be placed?

A

In the root directory of the module.

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

Given a module ‘zoo.animal.feeding’ with a file Task.java, in the root directory ‘test’ and a directory ‘mods’ containing custom module files.

Compile the code.

A

javac -p mods test/zoo/animal/feeding/Task.java test/module-info.java

or

javac –module-path mods test/zoo/animal/feeding/Task.java test/module-info.java

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

Given a module ‘zoo.animal.feeding’ with a file Task.java in the root directory ‘test’ and a directory ‘mods’ containing custom module files.

Compile the code and place the class files in the directory ‘out’.

A

javac -p mods -d out test/zoo/animal/feeding/Task.java test/module-info.java

or

javac –module-path mods -d out test/zoo/animal/feeding/Task.java test/module-info.java

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

Suppose there is a module named book.module which was compiled in the directory ‘test’. Inside that module is a package named com.sybex, which has a class named OCP with a main() method.

Using the above information, run the module.

A

java –module-path test –module book.module/com.sybex.OCP

–module-path can be replaced by -p and –module can be replaced by -m

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

Package everything under the ‘test’ directory to the mods directory under the name zoo.animal.feeding

A

jar -cvf mods/zoo.animal.feeding.jar -C test/ .

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

Given the following module-info.java file, export the zoo.animal.feeding module so that the module will be available for other modules.

module zoo.animal.feeding { }

A
module zoo.animal.feeding { 
    exports zoo.animal.feeding;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given the module zoo.animal.care, create a module-info.java file that exports the package zoo.animal.care.medical and uses the zoo.animal.feeding module.

A

module zoo.animal.care {
exports zoo.animal.care.medical;
requires zoo.animal.feeding;
}

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

Given the module zoo.animal.care, create a module-info.java file that exports the package zoo.animal.care.medical to only the module zoo.staff.

A
module zoo.animal.care {
    exports zoo.animal.care.medical to zoo.staff;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the private access control mean with multiple modules?

A

The private access modifier applied to an element does not allow any outside modules to access the element.

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

What does the default (package-private) access control mean with multiple modules?

A

The default access modifier applied to an element does not allow any outside modules to access the element.

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

What does the protected access control mean with multiple modules?

A

Accessible to subclasses only if package is exported

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

What does the public access control mean with multiple modules?

A

Accessible only if package is exported

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

What does ‘requires transitive’ do?

A

While ‘requires’ specifies that the current module depends on another module, ‘requires transitive’ specifies that the current module depends on another module plus all the modules that the other module requires.

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

Given the modules zoo.animal.feeding, zoo.animal.talks, zoo.animal.care and zoo.staff, does the following code compile?

module zoo.staff {
    exports zoo.staff;
    requires zoo.animal.feeding;
    requires zoo.animal.care;
    requires zoo.animal.talks;
    requires transitive zoo.animal.care;
}
A

Java doesn’t allow you to repeat the same module in a requires clause. It is redundant and most like an error in coding.

17
Q

What does the provides ‘keyword’ do?

A

The provides keyword specifies that a class provides an implementation of a service. (Think of a service as a fancy interface).

provides zoo.staff.ZooApi with zoo.staff.ZooImpl

18
Q

What does the provides ‘uses’ do?

A

The uses keyword specifies that a module is relying on a service.

uses zoo.staff.ZooApi

19
Q

What does the provides ‘opens’ do?

A

Allows any module using the module where opens is used to use reflection (inspect and call code at runtime_

20
Q

What command can we use to find out what the module structure is of a jar file?

A

java -p {module-path here} -d {module here}

or

java -p {module-path here} –describe-module {module here}

21
Q

What command can we use to list available modules?

A

java –list-modules
/
java -p mods –list-modules

22
Q

What flag can we use to print debugging information when running the modules?

A

–show-module-resolution

23
Q

What command can we use to get information about dependencies within a module that are actually used (and not just declared)?

A

The jdeps command

24
Q

Use a command to get information about actual dependencies of the module zoo.staff in the directory mods.

A

jdeps mods/zoo.staff.jar

25
Q

Use a command to get a summary of the information about actual dependencies of the module zoo.staff in the directory mods.

A

jdeps -s mods/zoo.staff.jar

or

jdeps -summary mods/zoo.staff.jar

26
Q

Name all important JMOD operations

A

create, extract, describe, list, hash