Java OCA OCP Practice Question 1242

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.ja  va  2s . c o  m
public class Main { 
        public void withdrawal(int v) {} 
        public void deposit(int v) {} 
} 
     ? 
package mypkg2; 
// INSERT CODE HERE 
public class Teller { 
        public void processAccount(int depositSlip, int withdrawalSlip) { 
          withdrawal(withdrawalSlip); 
          deposit(depositSlip); 
       } 
} 
  • A. import static mypkg.Main.*;
  • B. static import mypkg.Main.*;
  • C. import static mypkg.Main;
  • D. None of the above


D.

Note

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

In this case, the withdrawal() and deposit() methods in the Main class are not marked static.

They require an instance of Main to be used and cannot be imported as static methods.

Therefore, Option D is correct.

If the two methods in the Main class were marked static, then Option A would be the correct answer since wildcards can be used with static imports to import more than one method.

Option B reverses the keywords static and import, while Option C incorrectly imports a class, which cannot be imported via a static import.




PreviousNext

Related