1. What is a module in Java?
Answer:
Explanation:
In Java, a module is a self-contained unit of code and data with a well-defined interface, introduced to enhance modularity and maintainability of Java applications.
2. In which Java version was the module system introduced?
Answer:
Explanation:
The module system, also known as Project Jigsaw, was introduced in Java 9.
3. What is the primary file in a Java module?
Answer:
Explanation:
The module-info.java file is the primary file in a Java module, containing module declarations and dependencies.
4. Which keyword is used to declare a module in Java?
Answer:
Explanation:
The 'module' keyword is used at the beginning of the module-info.java file to declare a module.
5. How do you specify which packages a module makes available to other modules?
Answer:
Explanation:
The 'exports' keyword in the module-info.java file is used to specify which packages the module makes available to other modules.
6. What is the purpose of the 'requires' statement in a module declaration?
Answer:
Explanation:
The 'requires' statement in a module declaration is used to specify dependencies on other modules.
7. How do you use services provided by another module in Java?
Answer:
Explanation:
The 'uses' keyword in a module declaration is used to specify that the module uses a service provided by another module.
8. How do you specify that a module provides an implementation of a service?
Answer:
Explanation:
The 'provides…with…' syntax in a module declaration is used to specify that the module provides an implementation of a service.
9. What is a module descriptor in Java?
Answer:
Explanation:
The module descriptor refers to the module-info.java file, which contains declarations and directives for the module.
10. Can a module export two packages with the same name?
Answer:
Explanation:
In Java, a module cannot export two packages with the same name, as package names must be unique within a module.
11. What is module path in Java?
Answer:
Explanation:
Module path is the set of paths where Java runtime searches for modules, similar to how the classpath works for locating classes and jars.
12. Can you convert a traditional JAR file into a Java module?
Answer:
Explanation:
A traditional JAR file can be converted into a Java module by adding a compiled module descriptor (module-info.class) to it.
13. What is an automatic module in Java?
Answer:
Explanation:
An automatic module is a JAR file placed on the module path, which is treated as a module by the Java runtime, without an explicit module descriptor.
14. What is the 'transitive' keyword used for in a module declaration?
Answer:
Explanation:
The 'transitive' keyword in a 'requires' statement implies that the dependency is available transitively to other modules that require this module.
15. How do you encapsulate internal packages within a module?
Answer:
Explanation:
Packages that are not exported in the module-info.java file are encapsulated within the module and not accessible to other modules.