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








Question

Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile?


     //Shape.java
     package com.java2s; 
     public class Shape { } 


     //Employee.java
     package com.java2s.beans; 
     public class Employee { } 


     //
     package printer; 
    
     INSERT IMPORTS HERE 
    
     public class Printer { 
        public void admire(Employee jelly) { 
        } 
     } 
  1. import com.java2s.*;
  2. import com.java2s.*.Employee;
  3. import com.java2s.beans.Employee;
  4. import com.java2s.beans.*;
  5. import com.java2s.beans.Employee.*;
  6. None of these can make the code compile.




Answer



C, D.

Note

C is correct since it imports Employee by classname.

D is correct since it imports all the classes in the beans package, which includes Employee.

A is incorrect because it only imports classes in the com.java2s package-Shape in this case-and not those in lower-level packages.

B is incorrect since wildcards can only be used at the end of an import statement.

E is incorrect since we cannot import parts of a class with a regular import statement. We can use static import to import static methods and fields.