Java OCA OCP Practice Question 847

Question

Given the following class, which of these are valid ways of referring to the class from outside of the package com.mypkg?

package com.mypkg; 
public class Base{ 
   //  .... 
   // lot of code... 
} 

Select 2 options

  • A. Base
  • B. By importing the package com.* and referring to the class as mypkg .Base
  • C. importing com.* is illegal.
  • D. By importing com.mypkg .* and referring to the class as Base.
  • E. By referring to the class as com.mypkg .Base.


Correct Options are  : D E

Note

A class or interface can be referred to by using its fully qualified name or its simple name.

Using the fully qualified name will always work, but to use the simple name either the class must be in the same package or it has to be imported.

By importing com.mypkg .* all the classes from the package will be imported and can be referred to using simple names.

Importing com.* will not import the subpackage mypkg.

It will only import the classes in package com.




PreviousNext

Related