Java Modules MCQ Questions and Answers

1. What is a module in Java?

a) A function or method
b) A package of classes and interfaces
c) A framework for dependency management
d) A self-contained unit of code and data

Answer:

d) A self-contained unit of code and data

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?

a) Java 8
b) Java 9
c) Java 10
d) Java 11

Answer:

b) Java 9

Explanation:

The module system, also known as Project Jigsaw, was introduced in Java 9.

3. What is the primary file in a Java module?

a) module-info.java
b) module.java
c) module-config.java
d) module-manifest.java

Answer:

a) module-info.java

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?

a) module
b) export
c) require
d) package

Answer:

a) module

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?

a) Using the 'provides' keyword
b) Using the 'exports' keyword
c) Using the 'public' keyword
d) Using the 'open' keyword

Answer:

b) Using the 'exports' keyword

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?

a) To specify dependencies on other modules
b) To import packages from other modules
c) To include additional source files
d) To request specific Java version

Answer:

a) To specify dependencies on other modules

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?

a) Using the 'uses' keyword
b) Using the 'provides' keyword
c) Using the 'requires' keyword
d) Using the 'with' keyword

Answer:

a) Using the 'uses' keyword

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?

a) Using the 'provides...with...' syntax
b) Using the 'exports' keyword
c) Using the 'implements' keyword
d) Using the 'service' keyword

Answer:

a) Using the 'provides...with...' syntax

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?

a) A configuration file for module dependencies
b) Another name for the module-info.java file
c) A special annotation for modules
d) A file that describes module versioning

Answer:

b) Another name for the module-info.java file

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?

a) Yes
b) No
c) Only if they are in different directories
d) Only if one of them is a subpackage

Answer:

b) No

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?

a) A sequence of code within a module
b) The location where Java stores standard modules
c) The set of paths where the Java runtime searches for modules
d) The directory structure within a module

Answer:

c) The set of paths where the Java runtime searches for modules

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?

a) Yes, by adding a module-info.class file
b) No, it's not possible
c) Only if the JAR file is recompiled
d) Only for JAR files created with Java 9 or later

Answer:

a) Yes, by adding a module-info.class file

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?

a) A module that is automatically downloaded
b) A module that is automatically created from a JAR file
c) A module with automatically generated module-info.java
d) A module that is part of the Java standard library

Answer:

b) A module that is automatically created from a JAR file

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?

a) To make all required modules available to consumers of the module
b) To transitively export packages to other modules
c) To include transitive dependencies
d) To mark a module as being transitively available

Answer:

a) To make all required modules available to consumers of the module

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?

a) By not exporting them
b) By marking them as private
c) By using the 'internal' keyword
d) By placing them in a special directory

Answer:

a) By not exporting them

Explanation:

Packages that are not exported in the module-info.java file are encapsulated within the module and not accessible to other modules.


Comments