java packages interview questions

what is Java package?

OR

what is package in Java?

In general Term, package is like group of related items/things. e.g. if you have seen or eat biscuits . it biscuits comes in packet. biscuits group into the packet. similarly in java everything is a class. and package in java contains the related classes together.

The main purpose of packaging is to remove the headache of searching class. as we can group similar type class together in packages. it will help to organised our application code, if we have thousands of classes. package is very useful concept in java

Interview Questions for Package in java

Which package is always imported by default?

No. It is by default loaded internally by the JVM. The java.lang package is always imported by default.

Can I import same package/class twice? Will the JVM load the package twice at runtime?

One can import the same package or same class multiple times. Neither compiler nor JVM complains anything about it. And the JVM will internally load the class only once no matter how many times you import the same class.

Does importing a package imports the sub packages as well? E.g. Does importing com.bob.* also import com.bob.code.*?

No you will have to import the sub packages explicitly. Importing com.bob.* will import classes in the package bob only. It will not import any class in any of its sub package’s.

What is a Java package and how is it used?

Or

Explain the usage of Java packages.

A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces. Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces.
For example: The Java API is grouped into libraries of related classes and interfaces; these libraries are known as package.

Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.BOB compile?

Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying, cannot resolve symbol.

 

 

What are packages? What is use of packages?

The package statement defines a name space in which classes are stored. If you omit the package, the classes are put into the default package.
 

What is difference between importing “java.applet.Applet” and “java.applet.*”?
 “java.applet.Applet” will import only the class Applet from the package java.applet, where as “java.applet.*” will import all the classes from java.applet package.

 What do you understand by package access specifier?
 Public: Anything declared as public can be accessed from anywhere.
Private: Anything declared in the private can’t be seen outside of its class.
Default: It is visible to subclasses as well as to other classes in the same package.

By default, all programs import the java.lang package.
True.

 

What is the package name for FileNotFoundException class?

java.io

Name few classes of package java.io?

[ SecureDirectoryStream<T>,  ImageOutputStream,  AsynchronousChannel,  StringBufferInputStream,  ScatteringByteChannel, FileNotFoundException,  JavaFileManager,  InputStream,  ImageInputStream,  PipedInputStream,  StandardJavaFileManager,  SequenceInputStream, NetworkChannel,  MulticastChannel,  WritableByteChannel,  JMXConnector,  InterruptibleChannel,  ObjectInputStream,  SeekableByteChannel,  FilterInputStream,  DirectoryStream<T>,  RMIConnection,  GatheringByteChannel, AsynchronousByteChannel,  ByteChannel, InputStream,  Channel, IOException, FileInputStream,  ReadableByteChannel, AudioInputStream,  ByteArrayInputStream,  FileInputStream,  WatchService, File]

What is the package name for FileInputStream class?

java.io