Java Interview Questions 9 of 9 - Package Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a package?

A

The package is a grouping mechanism in which related class files are grouped and made available to other applications and other parts of the same application. So, the package is a collection of related classes and interfaces. The package declaration should be the first statement in a Java class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of package in Java?

A

A package is used to encapsulate a group of classes, interfaces and sub-packages. Often, it is a hierarchical structure of storing information. It is easier to organize the related classes and sub- packages in this manner.

A Package also provides access protection for classes and interfaces. A package also helps in removing naming collision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is java.lang package?

A

In Java, java.lang package contains the classes that are fundamental to the design of Java programming language. The most important class in this package is Object class.

It also contains wrapper classes like- Integer, Boolean, Character etc. It provides Math class for mathematical operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which is the most important class in Java?

A

It is an open-ended question with many answers. In my view, Object class is the most important class of Java programming language. It is the root of all the classes in Java. It provides some very important and fundamental methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is it mandatory to import java.lang package every time?

A

No. By default, JVM loads it internally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a standard package in Java?

A

The standard package is the package that contains all libraries or pre-defined class files. There are two types of standard packages:

Core packages (which are starts with Java)

Extension packages (Which are starts with Javax)

Example: For Core packages For Extension packages

  1. Java.lang Javax.sql
  2. Java.util Javax.servlet
  3. Java.awt Javax.servlet.http
  4. Java.applet Javax.servlet.jsp
  5. Java.io Javax.ejb
  6. Java.net Javax.swing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you import same package or class twice in your class?

A

If we import same package multiple times in a class, compiler includes it only once. So neither JVM nor Compiler gives any error/warning on including a package multiple times.

If you have two classes with same name, then you may get name collision on importing the class erroneously.

JVM internally loads the class only one time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a static import in Java?

A

Static import is similar to normal import declaration. Normal import allows us to import classes from packages without using package qualifier. Static import allows us to import static members from a class without using class qualifier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a user-defined package in Java?

A

The packages which are created by the users in the Java application development are known as “user-defined package”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to create a user-defined package?

A

The ‘Package’ keyword is used for creating a package in Java. Its syntax is:

package ;

As per industrial standards, the package name should follow the reverse domain naming convention. Example:

package org.companyname.projectname.modulename.submodule;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

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

Are the imports checked for validity at compile time, like: will the code containing an import such as Java.lang.ABCD be compiled?

Yes, the imports are checked for the semantic validity at compile time. The code containing Java.lang.ABCD will not compile. It will throw an error saying, cannot resolve symbol:

symbol: class ABCD
location: package io

import Java.io.ABCD;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between import static com.test.Fooclass and import com.test.Fooclass?

A

First import is a static import and the second import is normal import of a class. First import allows us to import static members of class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly