Java 9 - Interview Questions and Answers on Java 9

Q1. What is project Jigsaw in Java 9 ?

Ans. The projects aims to organize the Java source code into independent modules and hence facilitate building them independently.So now we will have multiple core java jars instead of single one. This way of organizing the code would offer following advantages

1. Will make applications lighter as only required modules will be built with the application. Moreover there are some legacy packages currently which anyone hardly use. 

2. Reuse same class whole identifiers across different modules.

Q2. What does the modularity in project jigsaw means ?

Ans. It means that now individual modules will be built independently instead of complete jdk built in a single jar file. So we will have multiple jars instead of single jar. Moreover this organization would result in lighter applications as they can just built themselves with the required jars and hence ignoring the not required and legacy modules.

Q3How Java 9 help resolve NoClassDefFoundError through it's modularity ?

Ans. Java 9 would provide an error at compile time if there are different modules with the same package mapped to the same class loader and hence would provide a compile time check for this issue.

NoClassDefFoundError exists because of ambiguity at realtime because of multiple versions of same package / class exist. As Java 9 would provide a compile time check for it through clearly defined dependencies and exports and a check on duplicate packages being loaded by single class loader, it will fix the problem.

Q4. How does Java 9 provides backward compatibility with libraries that haven't published themselves as modules ?

Ans. Though Automatic modules. Any JAR on the module path without module descriptor ends up as automatic module

Q5. What is the default setting for Automatic modules regarding exporting packages and reading modules ?

Ans. As one purpose of automatic module is to provide backward compatibility with the module less dependencies, automatic modules exports all packages and include all other modules.

Q6How does modularity in Java 9 facilitates better encapsulation ?

Ans. By providing dependencies and exports explicitly. 

Q7. How Java 9 helps in building light weight applications ?

Ans. By organizing the Java source code into independent modules and hence facilitate building them independently.

Q8. How modules are different than packages in Java 9 ?

Ans. Modules are the units that contains multiple packages and specify it's services , exports and dependencies. 

Q9. Why do we have multiple core java jars in java 9 ?

Ans. To provide modularity i.e individual jars for different modules.

Q10. Do we need different class loaders for different modules in Java 9 ?

Ans. No, We can use the same class loader.

Q11. What is ModuleLayer ?

Ans. A Module layer is created from a graph of modules and is a function that maps modules to their class loaders.

Q12. What is ModuleDescriptor ?

Ans. A module descriptor describes a named module and defines methods to obtain each of it's components.

Q13. Difference between named and unnamed modules ?

Q14. Does Java 9 allows same package within different modules ?

Ans. Yes, if different class loaders are mapped to those modules.

Q15. What are the qualified and unqualified packages ?

Q16. What is the use of Requires, Exports, Opens and provides within Module Descriptor ?

Ans. Requires specifies the dependencies for the module, i.e the modules required for this module to function.

Exports specifies the packages exported by the module.

Opens specifies the packages opened by the module.

Provides specifies the services provided by the module