OCA Java SE 8 Building Blocks - OCA Mock Question Building Block 18








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)


     //Shape.java
     package com.java2s; 
     public class Shape { 
       boolean printable = false; 
     } 

     //Shape.java
     package com.java2s.beans; 
     public class Shape { 
       boolean printable = true; 
     } 


     //ShapeFiller.java
     package filter; 
     
     INSERT IMPORTS HERE 
     
     public class ShapeFiller { 
       Shape water; 
     } 
  1. import com.java2s.*;
  2. import com.java2s.Shape; import com.java2s.beans.*;
  3. import com.java2s.*; import com.java2s.beans.Shape;
  4. import com.java2s.*; import com.java2s.beans.*;
  5. import com.java2s.Shape; import com.java2s.beans.Shape;
  6. None of these imports can make the code compile.




Answer



A, B, C.

Note

A is correct because it imports all the classes in the com.java2s package including com.java2s.Shape.

B and C are correct because they import Shape by classname.

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

D is incorrect because Java doesn't know which of the two wildcard Shape classes to use. They both have the Shape class.

E is incorrect because you cannot specify the same classname in two imports, which result in name confliction.