Java OCA OCP Practice Question 1203

Question

Given the following two classes, each in a different package, which line inserted below allows the second class to compile?

package mypkg; /*from ww w. j  av  a2 s.  c om*/
public class Main { 
        public static String getValue() { return "dress"; } 
} 
     ? 
package wardrobe; 
// INSERT CODE HERE 
public class Closet { 
        public void borrow() { 
           System.out.print("Borrowing mypkg: "+getValue()); 
        } 
} 
A.  static import mypkg.Main.getValue; 
B.  import mypkg.Main.*; 
C.  import static mypkg.Main.getValue; 
D.  import static mypkg.Main; 


C.

Note

Option A is incorrect because the keywords static and import are reversed.

The Closet class uses the method getValue() without a reference to the class name Main.

Therefore a static import is required.

For this reason, Option B is incorrect since it is missing the static keyword.

Option D is also incorrect since static imports are used with members of the class, not a class name.

Finally, Option C is the correct answer since it properly imports the method into the class using a static import.




PreviousNext

Related