Interview Questions On Packages In Java

Interview Questions On Packages In Java: In Java, a package is a group of related classes, interfaces, and sub-packages. Packages help developers organize their code and make it easier to maintain and reuse in multiple projects. As such, Packages Interview Questions are a critical part of Java developer interviews.

These interview questions aim to assess a candidate’s knowledge and proficiency in using Java packages, including their advantages, the different types of packages, and the best practices associated with their usage.

A clear understanding of Java packages is essential in developing efficient and maintainable Java programs. This article presents some commonly asked Package Interview Questions that can help Java developers prepare for interviews and better understand Java packages.

Java Packages Interview Questions and Answers

What is a package?
Ans: 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.

What is a Java package, and how is it used?
Ans: A Java package is a naming context for classes and interfaces. It creates a separate namespace for groups of classes and interfaces, organizes related classes and interfaces into a single API unit, and controls accessibility to these classes and interfaces.

What is the first keyword used in a Java application development?
Ans: The ‘Package’ is the first keyword used in a Java Application development.

Which package is imported by default?
Ans: By default, the ‘Java.lang’ package is imported, even without a package declaration.

Can a class declared as private be accessed outside its package?
Ans: No. Once declared private, accessing the class outside its package is impossible.

What is the naming convention to be followed for declaring a user-defined package in Java application development?
Ans: The reverse domain naming convention is to be followed when declaring a user-defined package in Java application development. For this, the syntax is:

com.companyname.projectname.module.submodule;

Example: com.apache.struts.tiles.login;

What are the types of packages in Java?
Ans: There are two types of packages in Java

  • Standard or Built-in packages and
  • User-defined packages.

What is a standard package in Java?
Ans: The standard package is the package that contains all libraries or pre-defined class files. There are two types of standard packages:

  1. Core packages (which start with Java)
  2. Extension packages (Which start 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

Does importing a package make sub-package class files available to the application?
Ans:  No. Importing a package does not make sub-package class files available to the application.
Example:

import Java.awt;
class TestPackage
{
	Button b;
	ActionEvent ae;
	/*it is an error bcoz.. ActionEvent is available in 
	 * Java.awt.event.So we have to import Java.awt.event.*; also*/
	public static void main(String args[])
	{
		System.out.println("Hello Packages");
	}
}

What is a user-defined package in Java?
Ans: The packages which are created by the users in the Java application development are known as “user-defined package”.

How to create a user-defined package?
Ans:  The ‘Package’ keyword is used to create 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;

package org.softwaretestingo.hrms.leavemangement;
//package: org.softwaretestingo.pacakgesprograms.userdefinedpackges;
class Employee
{
	int empId=30039;
	String empName="smith";
	float empSal=15000;
	public void getEmpDetails()
	{
		System.out.println("employe id: "+empId);
		System.out.println("employe name: "+empId);
		System.out.println("employe salary: "+empId);
	}
}

How to compile a source code of Java that is created as a package?
Ans: The command for compiling source code with special syntax is:

Javac –d . FileName.Java

Example: Javac –d . Employe.Java

What happens in the background when “Javac –d. Employe.Java “is executed?

Ans: When this command is executed the following things happen:

  • the org directory is created in the current working directory (or default package).
  • Employe.class file is stored in: org.talentsprint.pacakgesprograms.userdefinedpackges

What happens if we develop another Java class (called Department) and compile the class with the same package name? Does it create a new package with the same name?
Ans: No, instead, it will store the second.class file in the same package directory.
Example:

package org.softwaretestingo.pacakgesprograms.userdefinedpackges;
class Department
{
	int deptId=1234;
	public int getDeparmentId()
	{
		return deptId;
	}
}

After compilation, it will store the Department.class file into the package:
org.softwaretestingo.pacakgesprograms.userdefinedpackges;

How to use user-defined packages in Java?
Ans: There are two fundamental steps to access and use the user-defined packages:

Step-1: Set the classpath to the user-defined package, i.e. in which directory the user-defined package is stored up to that directory set the classpath. Example: set classpath =E:\softwaretestingo\myprograms (Assume that our programs are created in the above path)

Step 2: Using the import keyword, we can import all the specific user-defined and standard packages.
Example:

import org.softwaretestingo.pacakgesprograms.userdefinedpackges;
class RunProgram
{
	public static void main(String args[])
	{
		Employe e1=new Employe();
		System.out.println(e1.showEmpDetails();
		Department d1=new Department();
		System.out.println(d1.getDeptId());
	}
}

Why do we use user-defined packages?
Ans: We can use user-defined packages to:

  • Group related class files into a separate namespace.
  • Provide the same level of security
  • Make applications available to other parts of the application and in the same application also.

Can a source file contain more than one class declaration?
Ans: Yes. A single source file can contain any number of class declarations, but only one of the classes can be declared as public.

What restrictions are placed on the location of a package statement within a source code file?
Ans: A package statement must appear as the first line in a source code file (excluding blank lines and comments).

What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import Java.net.* versus import Java.net.Socket)?
Ans: There is no difference in the generated class files since only the classes that are actually used are referenced by the generated class file. The practical benefit of importing single classes can be realized when two (or more) packages with the same class name are used, like Java.util.Timer and Javax.swing.Timer.

If you import java.util.* and Javax.swing.* and then try to use “Timer”, you will get an error while compiling (the class name is ambiguous between both packages). Let’s say what you really wanted was the Javax.swing.Timer class and the only classes you plan on using in Java.util are Collection and HashMap. In this case, some people will prefer to import java.util.Collection and import java.util.HashMap instead of importing Java.util.*.

This will now allow them to use Timer, Collection, HashMap, and other Javax.swing classes without using fully qualified class names in.

Can I import the same package/class twice? Will the JVM load the package twice at runtime?
Ans: You can import the same package or class multiple times. Neither the compiler nor the 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? For example, will the code contain an import such as Java.lang.ABCD be compiled?

Yes, the imports are checked for 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;

Does importing a package import the sub-packages as well?
Ans: Example: Does import com.MyTest.* also import com.MyTest.UnitTests.*? No, you will have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of its subpackages.

Do I need to import Java.lang package any time? If yes, then why?
Ans: No. It is, by default, loaded internally by the JVM.

What is a reflection package?
Ans: The java.lang reflect package can analyze itself in runtime.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment