Java OCA OCP Practice Question 1360

Question

Given the following two classes,

each in a different package,

which lines allow the second class to compile when inserted independently? (Choose two.)

package mypkg2; /*  ww w .  j a  v  a 2 s.c  om*/
public class MyClass { 
   public static int count = 10; 
   public static MyClass getMyClass() {return new MyClass();} 
} 

package mypkg; 
// INSERT CODE HERE 
public class Main { 
   public void main(String[] argv) { 
      getMyClass(); 
      System.out.print(count); 
   } 
} 
A.   import static mypkg2.MyClass.getMyClass; 
     import static mypkg2.MyClass.count; 

B.   import static mypkg2.*; 

C.   static import mypkg2.MyClass.*; 

D.   import mypkg2.MyClass.*; 

E.   static import mypkg2.MyClass.getMyClass; 
     static import mypkg2.MyClass.count; 

F.   import static mypkg2.MyClass.*; 



    A, F.

    Note

    A static import is used to import static members of another class.

    Option A is correct because the method getMyClass and variable count are imported.

    Option F is also correct because a wildcard on the MyClass class for all visible static members is allowed.

    Option B is incorrect because the wildcard must be on a class, not a package.

    Option C and E are incorrect since the keywords import and static are reversed.

    Option D is incorrect because the static keyword is missing.




    PreviousNext

    Related