Search Tutorials


Java Modules MCQ Questions and Answers | JavaInUse

Java Modules MCQ Questions and Answers

Q. What is a Java Module?

A. A self-contained package that encapsulates a set of related classes and interfaces
B. A way to organize and structure Java code into separate functional units
C. A collection of Java files that can be compiled and executed independently
D. All of the above

Q. What is the main benefit of using Java Modules?

A. Improved performance
B. Better code organization and maintainability
C. Enhanced security
D. All of the above

Q. How do you declare a Java Module?

A. Using the module keyword followed by the module name
B. By creating a new file with a .module extension
C. Using the package keyword followed by the module name
D. None of the above

Q. What is the difference between a package and a module in Java?

A. A package is a collection of related classes, while a module is a collection of related packages.
B. A package is a way to organize classes, while a module is a way to organize packages.
C. A package is a physical grouping, while a module is a logical grouping.
D. All of the above

Q. How do you export a package from a module?

A. Use the exports keyword followed by the package name
B. Use the open keyword followed by the package name
C. Use the provides keyword followed by the package name
D. Use the requires keyword followed by the package name

Q. What is the purpose of the requires directive in a module?

A. To specify the dependencies of a module
B. To import classes from another module
C. To export packages from the current module
D. To open a package to all modules

Q. What is the difference between requires transitive and requires static in module declaration?

A. requires transitive includes the dependencies of the required module, while requires static does not.
B. requires transitive is used for runtime dependencies, while requires static is used for compile-time dependencies.
C. requires transitive is used for optional dependencies, while requires static is used for mandatory dependencies.
D. requires transitive includes all modules in the module path, while requires static includes only the specified module.

Q. How do you provide services in a Java Module?

A. Use the provides keyword followed by the service interface and implementation class
B. Implement the service interface and export the implementation class
C. Use the services keyword followed by a list of service provider classes
D. Use the uses keyword followed by the service interface

Q. What is the purpose of the open keyword in a module declaration?

A. To make a package accessible to all modules
B. To allow classes from other modules to access the package
C. To make the package public and allow external access
D. All of the above

Q. How do you use classes from another module in your code?

A. Import the class using the import statement
B. Require the module using the requires directive
C. Export the class from the other module
D. Open the package containing the class





Q. What is the difference between strong encapsulation and weak encapsulation in Java Modules?

A. Strong encapsulation restricts access to a module's packages, while weak encapsulation allows open access.
B. Strong encapsulation requires explicit exports and requires directives, while weak encapsulation does not.
C. Strong encapsulation provides better security, while weak encapsulation allows more flexibility.
D. All of the above

Q. How do you create a layered architecture using Java Modules?

A. Define modules for each layer and specify dependencies using requires directives
B. Use the layered keyword to group modules into layers
C. Implement each layer as a separate module and export packages accordingly
D. Use the provides keyword to specify the layers and their dependencies

Q. What is the purpose of the uses keyword in a module declaration?

A. To specify the services used by the module
B. To indicate that the module uses a specific version of Java
C. To declare that the module uses a specific set of libraries
D. To specify the modules that use the services provided by the current module

Q. How do you compile and run a Java Module?

A. Use the javac and java commands with the --module option
B. Compile and run the module as a regular Java application
C. Use the jmod tool to create a module and then run it
D. Use the jar command with the --module-version option

Q. What is the purpose of the --patch-module option in the javac command?

A. To specify a directory as a patch to an existing module
B. To create a patch module for an existing module
C. To apply a patch to a module during compilation
D. To specify a module as a patch to another module

Q. Which of the following code snippets demonstrates the correct way to create a Java module and export a package?

A.
module com.example.module {
    exports com.example.package;
}
B.
module com.example {
    opens com.example.package;
}
C.
module com.example.module {
    provides com.example.package;
}
D.
module com.example {
    exports com.example.module;
}

Q. What is the purpose of the "requires" keyword in a Java module declaration?

A. To specify the modules that the current module depends on
B. To define the packages that the current module exports
C. To specify the services provided by the current module
D. To define the packages that the current module opens for reflection

Q. Which of the following code snippets demonstrates the correct way to provide a service implementation in a Java module?

A.
module com.example.module {
    provides com.example.service.ServiceInterface with com.example.service.ServiceImpl;
}
B.
module com.example.module {
    provides ServiceInterface with ServiceImpl;
}
C.
module com.example.module {
    provides com.example.service.ServiceImpl;
}
D.
module com.example.module {
    provides com.example.service.ServiceInterface = com.example.service.ServiceImpl;
}

Q. What will be the output of the following code snippet?

module com.example.module {
    exports com.example.package;
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Modules!");
    }
}
A.
Hello, Modules!
B.
Error: Module "com.example.module" not found
C.
Error: Main class not found
D.
Error: Incompatible module version

Q. Which of the following code snippets demonstrates the correct way to use a service provided by another module?

A.
module com.example.client {
    requires com.example.service;
    uses com.example.service.ServiceInterface;
}
B.
module com.example.client {
    requires com.example.service;
    provides com.example.service.ServiceInterface;
}
C.
module com.example.client {
    requires com.example.service;
    uses com.example.service.ServiceImpl;
}
D.
module com.example.client {
    requires com.example.service;
    provides com.example.service.ServiceImpl;
}

Q. What is the purpose of the "opens" keyword in a Java module declaration?

A. To make a package accessible to other modules
B. To provide a service implementation to other modules
C. To specify the modules that the current module depends on
D. To open a package for reflection by other modules

Q. Which of the following code snippets demonstrates the correct way to use reflection with a module?

A.
module com.example.module {
    opens com.example.package;
}

public class Main {
    public static void main(String[] args) {
        Class<?> clazz = Class.forName("com.example.package.MyClass");
        Object obj = clazz.getDeclaredConstructor().newInstance();
        System.out.println(obj);
    }
}
B.
module com.example.module {
    exports com.example.package;
}

public class Main {
    public static void main(String[] args) {
        Class<?> clazz = Class.forName("com.example.package.MyClass");
        Object obj = clazz.getDeclaredConstructor().newInstance();
        System.out.println(obj);
    }
}
C.
module com.example.module {
    provides com.example.package;
}

public class Main {
    public static void main(String[] args) {
        Class<?> clazz = Class.forName("com.example.package.MyClass");
        Object obj = clazz.getDeclaredConstructor().newInstance();
        System.out.println(obj);
    }
}
D.
module com.example.module {
    requires com.example.package;
}

public class Main {
    public static void main(String[] args) {
        Class<?> clazz = Class.forName("com.example.package.MyClass");
        Object obj = clazz.getDeclaredConstructor().newInstance();
        System.out.println(obj);
    }
}

Q. What will be the output of the following code snippet?

module com.example.module {
    requires java.sql;
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Using Java SQL Module");
    }
}
A.
Using Java SQL Module
B.
Error: Module "java.sql" not found
C.
Error: Incompatible module version
D.
Error: Main class not found

Q. Which of the following code snippets demonstrates the correct way to create a modular JAR file?

A.
jar --create --file module.jar module-info.class com/example/package/MyClass.class
B.
jar --create --file module.jar module-info.java com/example/package/MyClass.java
C.
jar --create --file module.jar module-info.class src/main/java/com/example/package/MyClass.java
D.
jar --create --file module.jar module-info.java src/main/java/com/example/package/MyClass.class

Q. What is the purpose of the "module-path" option when running a Java application?

A. To specify the path to the application's main class
B. To define the path to the application's module descriptor
C. To specify the path to the application's module path
D. To define the path to the application's class files