Java OCA OCP Practice Question 531

Question

Given the following classes,

which of the following snippets can be inserted in place of INSERT IMPORTS HERE and have the code compile?

Choose all that apply

package mypkg; /*from  w ww.j  a  v  a  2  s . c om*/
public class MyClass { 
  boolean salty = false; 
} 


package mypkg.jellies; 
public class MyClass { 
  boolean salty = true; 
} 


package employee; 
INSERT IMPORTS HERE 
public class MyClassFiller { 
  MyClass water; 
} 
A.   import mypkg.*; 

B.   import mypkg.MyClass; 
     import mypkg.jellies.*; 

C.   import mypkg.*; 
     import mypkg.jellies.MyClass; 

D.   import mypkg.*; 
     import mypkg.jellies.*; 

E.   import mypkg.MyClass; 
     import mypkg.jellies.MyClass; 

F.   None of these imports can make the code compile. 


A, B, C.

Note

Option A is correct because it imports all the classes in the mypkg package including mypkg.MyClass.

Options B and C are correct because they import MyClass by classname.

Since importing by classname takes precedence over wildcards, these compile.

Option D is incorrect because Java doesn't know which of the two wildcard MyClass classes to use.

Option E is incorrect because you cannot specify the same classname in two imports.




PreviousNext

Related