Java OCA OCP Practice Question 2959

Question

Given the following three files:

2. package mypkg;  
3. import mypkg.modules.Rectangle;  
4. public class Main {  
5.   public static void main(String[] args){  
6.     Rectangle lunarShape = new Rectangle();  
7.     System.out.println(lunarShape);  
8. } }  //from  www.  j a  v  a2s. co  m
 
2. package mypkg.modules;  
3. public interface Shape { /* more code  */ }  
 
2. package mypkg.modules;  
3. public class Rectangle implements Shape { /* more code */ } 

And given that Shape.java and Rectangle.java were successfully compiled and the directory structure is shown below:

$ROOT  
  |-- mypkg  
  |       |-- modules  
  |               |-- Rectangle.class  
  |-- controls.jar  
  |             |-- mypkg  
  |                    |-- modules  
  |                            |-- Shape.class  
  |-- Main.java 

Which are correct about compiling and running the Main class from the $ROOT directory?

Choose all that apply.

  • A. The command for compiling is javac -d . -cp . Main.java
  • B. The command for compiling is javac -d . -cp controls.jar Main.java
  • C. The command for compiling is javac -d . -cp .:controls.jar Main.java
  • D. The command for running is java -cp . mypkg.Main
  • E. The command for running is java -cp controls.jar mypkg.Main
  • F. The command for running is java -cp .:controls.jar mypkg.Main
  • G. The command for running is java -cp controls.jar -cp . mypkg.Main


A, C, and F are correct.

Note

In order to successfully compile the Main class, you ONLY need the Rectangle class to be on the classpath, so A and C are correct.

F is correct, because in order to successfully run the Main class, the classpath should include BOTH the JAR file and the base location of the Main and Rectangle classes.

Here, the JAR file is required because the Rectangle class has to access the Shape class at runtime.

When specifying multiple locations for the classpath, each location should be separated with the ":" symbol, although in real life that character depends on the platform.




PreviousNext

Related