1Z0-811 Java Development and Deployment 2 — Questions and Answers
Question 1: Which command compiles a Java source file named `Hello.java` and places the output in a directory called `out`?
- javac -d out Hello.java (Correct answer)
- java -d out Hello.java
- javac -cp out Hello.java
- java -classpath out Hello.java
Correct answer: javac -d out Hello.java
The `javac -d <dir>` option specifies the destination directory for compiled `.class` files.
Question 2: What is the result of running `java -jar app.jar` when the manifest file has no `Main-Class` attribute?
- An error is thrown indicating no main manifest attribute (Correct answer)
- The JVM searches for a class named Main
- The first class in the JAR is executed
- The program runs with an empty entry point
Correct answer: An error is thrown indicating no main manifest attribute
Without a `Main-Class` attribute in MANIFEST.MF, the JVM cannot determine the entry point and reports an error.
Question 3: Which module directive is used to allow reflective access to all packages in a module by a specific other module?
- opens <package> to <module> (Correct answer)
- exports <package> to <module>
- requires transitive <module>
- provides <service> with <impl>
Correct answer: opens <package> to <module>
The `opens` directive grants reflective access (e.g., for frameworks) while `exports` only grants compile/run-time API access.
Question 4: What does the `javap -c` command display for a `.class` file?
- Disassembled JVM bytecode instructions (Correct answer)
- The source code of the class
- The class hierarchy
- The JAR manifest contents
Correct answer: Disassembled JVM bytecode instructions
`javap -c` disassembles and prints the bytecode instructions for each method in the class file.
Question 5: In a multi-release JAR, where should Java 11-specific class files be placed?
- META-INF/versions/11/ (Correct answer)
- META-INF/java11/
- versions/11/
- lib/java11/
Correct answer: META-INF/versions/11/
Multi-release JARs place version-specific classes under `META-INF/versions/<N>/` where N is the Java version.
Question 6: Which `java` command-line option sets the module path?
- --module-path (Correct answer)
- --classpath
- --module
- --add-modules
Correct answer: --module-path
`--module-path` (or `-p`) specifies where the JVM looks for named modules, analogous to `-classpath` for the legacy class path.
Question 7: What happens when you use `requires transitive com.example.util;` in a module descriptor?
- Modules that require your module automatically also read com.example.util (Correct answer)
- com.example.util is bundled inside your module's JAR
- Your module exposes all packages of com.example.util publicly
- com.example.util is loaded lazily at runtime
Correct answer: Modules that require your module automatically also read com.example.util
`requires transitive` implies that any module depending on your module implicitly also depends on `com.example.util`, enabling implied readability.
Which command compiles a Java source file named `Hello.java` and places the output in a directory called `out`?