1Z0-811 Java Development and Deployment 1 — Questions and Answers
Question 1: What is the purpose of the "module-info.java" file in a Java module?
- To define the entry point of the Java application
- To specify the modules that the current module depends on and the packages it exports (Correct answer)
- To manage the Java Virtual Machine (JVM) configuration
- To handle logging and error reporting
Correct answer: To specify the modules that the current module depends on and the packages it exports
The `module-info.java` file is the module descriptor in the Java Platform Module System (JPMS). It explicitly defines a module's name, declares its dependencies on other modules using `requires`, and specifies which of its packages are accessible to other modules using `exports`. This file is crucial for enforcing strong encapsulation and reliable configuration in modular applications.
Question 2: How does Java handle memory management and garbage collection?
- By manually allocating and freeing memory for objects
- By using a garbage collector to automatically reclaim memory from objects that are no longer in use (Correct answer)
- By requiring developers to manage memory allocation through pointers
- By using fixed-size memory blocks that are pre-allocated
Correct answer: By using a garbage collector to automatically reclaim memory from objects that are no longer in use
Java handles memory management automatically through a process called garbage collection. The garbage collector continuously monitors memory and reclaims space occupied by objects that are no longer referenced or reachable by the running application. This frees developers from manual memory allocation and deallocation, reducing memory leaks and improving application stability.
Question 3: What does the "javac" command do?
- It executes a Java application
- It compiles Java source code into bytecode (Correct answer)
- It packages Java bytecode into a JAR file
- It starts the Java Virtual Machine (JVM)
Correct answer: It compiles Java source code into bytecode
The `javac` command is the Java compiler. Its primary function is to translate Java source code files (with a `.java` extension) into Java bytecode files (with a `.class` extension). These bytecode files are then executed by the Java Virtual Machine (JVM).
Question 4: What is a JAR file in Java?
- A file used to define Java classes in a specific module
- A compressed file format used to bundle multiple Java class files and associated metadata into a single archive (Correct answer)
- A configuration file used to set JVM parameters
- A documentation file generated by JavaDoc
Correct answer: A compressed file format used to bundle multiple Java class files and associated metadata into a single archive
A JAR (Java Archive) file is a compressed file format, based on the ZIP format, used to bundle multiple Java class files, associated metadata, and resources (like images or configuration files) into a single archive. This makes it convenient for distributing and deploying Java applications and libraries as a single unit.
Question 5: What is the -classpath option used for when running the java command?
- To specify the path where Java source files are located
- To set the path for finding Java libraries and classes during runtime (Correct answer)
- To define the location of the module-info.java file
- To configure the Java Virtual Machine (JVM) memory settings
Correct answer: To set the path for finding Java libraries and classes during runtime
When running the `java` command, the `-classpath` (or `-cp`) option is used to specify the path where the Java Virtual Machine (JVM) should look for user-defined classes and packages. This path includes directories containing `.class` files and JAR files, ensuring that the JVM can locate all necessary components for the application to execute correctly at runtime.
What is the purpose of the "module-info.java" file in a Java module?