Type Here to Get Search Results !

Top 1000 Java Interview Questions & Answers - Section IX - Package in Java

1

 


We are sharing the top 1000 Java interview questions , these questions are frequently asked by the recruiters. Java interview questions can be asked from any core java topic . So here we tried our best to provide you the java interview questions and answers for experienced which should be in your to do list before facing java questions in  technical interview. 

In this Section we have the top selected interview questions on Package's in Java . A Package in java is used in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc. A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations ) providing access protection and namespace management.

Each question here about Package in java is accompanied with an answer so that you can prepare for job interview in short time.We have compiled this list after attending dozens of technical interviews in top-notch companies like- Facebook, Amazon,Oracle, Netflix, Microsoft etc.

Once you go through them in the first pass, mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions.After going through this book 2-3 times, you will be well prepared to face a technical interview for a Java Developer position from Software Engineer level to Principal Engineer level.

All the best!

Section IX - Package in Java


92. What is a package?

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.


93. What is the purpose of package in Java?

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.


94. What is java.lang package?

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.


 

95. Which is the most important class in Java?

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.


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

No. By default, JVM loads it internally.


97. What is a standard package in Java?

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




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

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.


99. What is a static import in Java?

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.


100. What is a user-defined package in Java?

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




101. How to create a user-defined package?

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;

package org.codinghumans.hrms.leavemangement;

//package: org.codinghumans.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);

   }

}


102. Can I import the 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 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;


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

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.

Recommended Posts

Top 1000 Java Interview Questions & Answers - Section VII - Abstraction

Top 1000 Java Interview Questions & Answers - Section VI - Polymorphism

Top 1000 Java Interview Questions & Answers - Section V - Method Overloading and Overriding

Top 1000 Java Interview Questions & Answers - Section I - Java Basics

Top 1000 Java Interview Questions & Answers - Section II - OOPs ( Object Oriented Programming )

Top 1000 Java Interview Questions & Answers - Section III - Inheritance

Top 1000 Java Interview Questions & Answers - Section IV - Static

Core Java Important Questions Part 1

Core Java Important Questions Part 2

Core Java Important Questions Part 3

Core Java Important Questions Part 4

Core Java Important Questions Part 5

Core Java Important Questions Part 6

Core Java Important Questions Part 7

Core Java Important Questions Part 8

Core Java Important Questions Part 9

Java Collections Interview Questions Part 1

Java Collections Interview Questions Part 2

Java Collections Interview Questions Part 3

Java MCQs with Answers Set I

Java MCQs with Answers Set II

Java MCQs with Answers Set III

If you have any doubts join the discussion below ,our Moderator will reply to your Queries

Post a Comment

1 Comments
  1. Sands Casino, Resort & Spa | New York, NY - SEGA
    Casino & Resort Spa, New York, NY. The iconic Sands Hotel Casino, Spa, is an ideal location 바카라 for 바카라사이트 true NY getaways. septcasino We offer luxury accommodations, world-class

    ReplyDelete
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad