Java OCA OCP Practice Question 1913

Question

Consider the following two java files:

//in file MyClass .java
package x.y;/*w ww .  j  a  v a2s  .  c om*/
public class MyClass {
    public static void foo (){  };
}

//in file Main.java
//insert import statement here //1
public class Main{
   public static void main (String [] args){
      foo ();
    }
}

What should be inserted at // 1 so that Main will compile and run?

Select 2 options

  • A. import static x.y.*;
  • B. import static x.y.MyClass;
  • C. import static x.y.MyClass.foo;
  • D. import static x.y.MyClass.foo ();
  • E. import static x.y.MyClass.*;


Correct Options are  : C E

Note

For A.

x.y.* means all the classes in package x.y. Classes cannot be imported using "import static".

You must do import x.y.* for importing class.

Further, importing a class will not give you a direct access to the members of the class.

You will need to do MyClass.foo(), if you import MyClass.

For B.

x.y.MyClass means you are trying to import class x.y.MyClass. A class cannot be imported statically.

For C.

This is valid because this statement is importing the static member foo of class x.y.MyClass.

For D.

Even though foo is a method, you cannot put () in the import statement.

For E.

This is valid because this statement is importing all the static members of class x.y.MyClass.




PreviousNext

Related