Java Interview Questions

Java Interview Questions And Answers

Java is a widely used computer programming language, and it is recommended that learners must familiarize themselves with it. The following basic core Java interview questions and answers for freshers strives to provide a cogent overview of Java, besides describing key principles of this computer programming language.


Java combines both a computing platform and a programming language into an integrated environment. It is a general-purpose programming language that is class-based and object-oriented, as well as supports concurrent computing. A programming language allows a programmer to communicate commands or instructions to a computer.

As a general-purpose programming language, a software developer can write a program in Java using one computer, and be able to run the same program on another different computer, even if it runs a different operating system. This also means that Java allows computers to communicate with each other, hence its use in developing web applications.

Object-oriented programming allows a programmer to define data types in a data structure, as well as the types of operations (called functions) that are to be applied to the data structure. The data structure is thus described as an object because it incorporates both data types and functions. Class-based programming allows the developer to define the classes of objects.

Concurrent computing means that more than one computation can be run at the same time. Unlike other programming languages, Java has few dependencies, which means that it can run in computers that have installed java-supported platforms. Dependencies are additional programs that a software needs so that it can run in a machine. Java was developed by Sun Micro systems, and it debuted in 1995.

What is Lexical Error?

A lexical error refers to any input which the lexer could reject. A lexer is a machine which accepts input strings that are valid, so, a lexical error will be any input strings which don’t result in the machine getting to an accepting state. When your lexer is no longer able to continue, it will throw out errors which are known as lexical errors meaning that there isn’t a way for your lexer to identify a lexeme as a valid token.

What is Intent Error?

This is also called a logic error and occurs when your code runs and compiles without crashing but the result is not what you intended. This is typically discovered because the output will be incorrect. The method for debugging intent errors is the same as that for debugging run-time errors. To do this, place print statements at strategic points in your code to find out the errors cause, or you can use DrJava to trace the execution of the program.

What is Abstract Window Toolkit?

Java programmers use Abstract Window Toolkit (AWT) to make objects like scroll bars, buttons, and windows. Abstract Window Toolkit is a feature of the Java Foundation Classes led by the company that designed Java, Sun Microsystems. AWT is the original platform-dependent graphics, windowing and widget toolkit by Java.

What is Cargo?

Cargo is a thin wrapper which gives you the ability to manipulate, in a standard way, application containers such as Java EE. The following tools and APIs are provided by cargo:
• A Java API which can configure, start, or stop supported containers
• A Java API that can deploy modules into the containers either locally or remotely
• A Java API which can merge, create, or parse J2EE modules
• ANT tasks which wrap the Java API for starting, stopping, configuring, and deploying applications to supported containers
• A Web interface which can wrap the Java API so that it can remotely start, stop, and configure all containers which are supported by Cargo
• Maven2/Maven3 plugins which wrap the Java API for starting, stopping, deploying and configuring applications to supported containers, and for creating, merging and parsing J2EE modules.

What is FIFO?

FIFO is an acronym for First-In, First-Out. FIFO is a valuation and asset management process whereby assets acquired or produced first are sold, disposed of, or used first. FIFO assumes that assets remaining in inventory are matched up with the assets that have most recently been produced or purchased for the purposes of taxation. FIFO is used for the purposes of cost flow assumption.

As items being manufactured are progressing to the later development stages and as inventory items that are finished are sold, the costs associated with the product must be identified as expenses. When this happens, the total value of the inventory decreases as the inventory is no longer under the company’s ownership. The FIFO method is one way to calculate costs associated with inventory.

What is Scaffolding?

Scaffolding is a meta-programming process which builds software applications that are backed by a database. It is a method which is supported by certain model-view-controller frameworks where a programmer can determine how an application database can be used by writing a specification. This specification can then be used to generate code which an application can use to read, update, create, and delete entries into the database. The template is treated as a scaffold on top of which a more powerful application can be built.

What are Access Specifiers?

Access specifiers are also called access modifiers. A simple definition would describe access specifiers as keywords in a programming language that form distinct and integral part of the programming language syntax.

Access modifiers set the accessibility of specific variables of a program by different elements of a program. In other words, they control and determine which program components have the right to the access of certain definite data.

What is Run time Polymorphism?

Run time polymorphism is a method override mechanism through which function binding and resolving of an overridden function take place at a run-time.This polymorphism is also referred to as dynamic polymorphism. In this type of polymorphism, the proper compilation time method which is called at run-time is established by the Java virtual machine and not the compiler.

Both the method overriding and the method overloading are excellent examples of run time polymorphism.

What is a HashSet?

Put in simple terms; a HashSet is an un ordered collection of unique elements with standard collection operations. A HashSet can both extend AbstractSet class and also implement the Set interface.

The information contained in a HashSet is stored using the hashing mechanism in a hash table. Together with HashMap, HashSet forms an integral and essential part of Java collection framework.

What is a TreeSet?

A TreeSet is a mechanism that gives a means for the implementation of a Set interface. In so doing, it guarantees the sorting of elements of a set.It is essential to note that the Set interface to be implemented usually uses tree storage. The storage of the objects is normally by sorting by ascending order, natural order or by a particular order specified by the programmer.

What is an EnumSet?

An EnumSet can be defined as a uniquely-designed Set implementation enum instance of an explicit enum type. In an EnumSet, all the elements contained therein are required to come from either an explicitly or implicitly specified single enum type.

This type of Set implementation is normally more specialized and efficient in comparison to both the TreeSet and the HashSet Set implementations.

What is a Package?

In Java, a package refers to a namespace whose function is to organize by grouping related class and interface types. A package guarantees both access protection and as well as namespace management.

Packages provide a reliable and efficient mechanism to summarize a collection of classes, interfaces as well as sub-packages. By default, use of packages provides a simple technique to categorize class files.

Describe the Environmental setup?

Java programming environment can be set up to run either online or in the local machine, that is, the computer. There are various online Java programming environment including Coding Ground that can be setup by following laid-down instructions. This section will focus on environmental setup in a local machine.

First of all, the programmer must download a Java Development Kit (JDK) that is compatible with the operating system in use. JDK provides the development environment upon which Java applications can be developed and tested. The development kit can be downloaded from the official website of Java. Next, the development kit is installed in the computer. Thereafter, environmental variables are set so that they point to the right installation directories.

 What is Garbage Collection?

Garbage collection is the process of recovering unused memory automatically. Memory management is a crucial work for any programming language. It is a technique of clearing unused objects, makes the memory so as not to lead to exhaustion. The deleted objects are either not in use or are out of reach to the user. Any object that has no reference is assumed to be useless, and the memory which is occupied by the object need to be released.
Garbage collection has various advantages, which includes:

It is a process done automatically It helps to increase the memory efficiency It decreases memory leak as objects not referenced are removed.

What is Callback?

A callback is a piece of code, which is referenced and executable code that you pass as an argument to some other code. By passing, the other code is expected to callback or execute at a given time. A callback holds references to some state to be useful.

This enables a lower level software layer to call a function defined in a higher-level layer. Indirection gained between your code and the code that is executing the callback by making the implementation dependent on your code. The function pointers, in this case, implemented as command objects.

 What is Aliasing?

Aliasing refers to a situation in which data location in the memory is accessed through a different symbolic name in the program. This makes it possible to have multiple variables that refer to the same object. Multiple aliases to a location can be updated during run-time.

 What is a Pseudo code?

A pseudo code refers to a simple tool that can express the design of a computer program or algorithm. It is a readable description of what a computer must do expressed in a formal and natural language rather than in a programmed language. It is mostly used one of the steps in developing a program. It allows programmers to express design in a detailed template in a specific programming language.

What is a Cargo?

Cargo refers to a thin wrapper that allows the programmer to manipulate and control various types of application containers such as Java EE. It provides a reliable way to deploy web applications in a running container. This is achieved by issuing a command and changing directions to the web application projects you want to manage.
The Cargo configuration does not change for all versions of any supported container.

What is Constant Time?

Constant time means that accessing any element whether first or last in an array takes the same amount of time. Arrays are random access where you get an element given an index by using a few machine code instructions. The time is constant because if obtaining one address in RAM takes three cycles that mean the next will also take three cycles.

What is Meta class?

A meta class is an object that defines the behavior of a particular type of classes. It is used to represent the types of classes and interfaces. It defines the data structure and methods of class description. In Java, you have one class description for each loaded class at run time. It allows you to change the structure and behavior of your class description.

What is Graphical User Interface?

Graphical user interface is a software that works on the point of touch (interface) between a computer and its user, and which employs picture factors (conversation packing containers, icons, menus, scroll bars) as opposed to textual content characters to let the computer user to provide instructions to the computer or to control what is on the monitor.

GUI factors are commonly accessed via a pointing device together with a mouse, pen, or stylus. All packages going for walks below a GUI use a consistent set of graphical elements so that once the user learns a particular interface, he or she can use all applications without gaining knowledge of extra or new commands.

What is Hot java(TM) Browser?

Hot Java is a web browser that makes the net “come alive.” Hot Java builds at the net surfing strategies set up via
Mosaic(tm) and expands them through implementing the capability to feature arbitrary conduct, which transforms static information into dynamic packages. The information viewed in other browsers is restrained to textual content,
illustrations, low-exceptional sounds and films.

The usage of Hot Java Browser could help upload applications that range from interactive science experiments in academic material to games and specialized purchasing programs.One can implement interactive advertising and customized newspapers; the possibilities are limitless.

Similarly, Hot Java offers a way for users to get right of entry to those packages in a new way. Software transparently migrates throughout the network. There is no such element as “putting in” software program. It simply comes when you need it. Content developers for the arena wide web do not need to fear about whether or not or no longer a few
special piece of software is hooked up in a consumer’s device; it simply received there mechanically. This transparent acquisition of packages frees developers from the boundaries of the constant media types like photos and text and permits them to do something they’d like.

What is JAR file format?

JAR stands for Java Archives. It’s a record format primarily based on the famous ZIP record format and is used for aggregating many documents into one. Even though JAR can be used as a fashionable archiving device, the primary motivation for its development was in order that Java applets and their considered necessary additives may be downloaded to a browser in a unmarried HTTP transaction, in preference to starting a new connection for every piece.

In case one would want to take advantage of advanced functionality provided through the JAR file format which include electronic signing, one may first want to become familiar with the fundamental operations. To carry out basic tasks with JAR files, you use the Java Archive device provided as part of the Java development kit (JDK).Due to the fact the Java Archive device is invoked with the aid of using the JAR command, this educational refers to it as ‘the Jar device’.

What is Java Beans (TM)?

Java Beans (TM) is what makes it smooth to reuse software program additives. Exclusive software developers can consequently use software additives written by way of others while not having to understand their internal workings or operations.

What is Java signature?

A signature is a listing that specifies a category constructor, an example technique, or a static approach, thereby distinguishing it from different constructors, example methods, or static methods. Signatures are of two types: easy and complete. A simple signature is a single detail list containing the call of the approach or constructor.

In most instances a simple signature is all that is wished as the Java technique resolver is capable of disambiguating overloaded Java techniques based totally at the varieties of Java object arguments. There are a few instances where the Java method resolver is unable to determine which Java technique one intendeds to invoke so that one will need to use the whole signature for the method or constructor. The overall signature is used to differentiate among
or extra methods or constructors which have the equal variety of arguments. The whole signature of a method is a TCL listing containing the method name observed through the name of the Java item type for every parameter
of the approach.

What is Java mixture (TM )?

A product that permits developers to simplify database utility development by mapping database records to objects within the JavaTM programming language (Java gadgets) and Java objects to databases.

Explain the Basic Syntax?

The following facts must be considered when developing or running a Java program.

Case Sensitivity – the program is case sensitive, and this means that the programmer must check the capitalization of identifiers as capitalization changes the meaning. For example, Hello and hello are different identifiers.

Class Names – the first letter of the class name must be capitalized. If several words are combined to form a class name, then the first letter of each word must be capitalized.

Method Name – the first letter of the class name must be in lower case. If several words are combined to form a method name, then the first letter of first word must be in lower case, while the first letter of all the subsequent words must be capitalized.

Program File name must match (exactly) the method name.

main() method – Program processing starts at the main() method

What are Object and Classes?
In a Java program, different object communicate with each other. This communication occurs when the objects invoke the methods of each other. To understand methods, there is need to understand object and class. Each object has its unique state and behavior. The behavior is also described as the method. Therefore, objects communicate when their respective behaviors are invoked. A class is the blueprint, or template, that describes the state and behavior of the objects that it supports. A class is declared by a class keyword.Each object has specific instance variables. Instance variables are assigned values that determine the state of an object.

Describe the Basic datatypes?
There are two basic datatypes in Java; primitive datatypes and reference datatypes.
Primitive datatypes are described below:

Boolean – In these datatypes, the value is either true or false.
Char – this is a character type and uses the 16-bit uni-code character.
Arithmetic types – They are also called numeric datatypes, and is divided into integral types and floating-point types.
Integral types – there are four different types:





Floating-point types – They are either float or double. A reference datatype is created through a reference variable.

Describe the Variable types?

A variable provides a Java program with a named storage that can be manipulated. Each variable has its own specific type that determines the layout and size of the memory associated with the variable, as well as the values that it can store. Also, the variable type determine sets of operations that are applicable to the variable.

There are three variable types in Java, and they are:

Local variable: It is declared in the method, and is only created when the method is invoked. It is destroyed when it exits its method.
Instance variable: It is declared in the class, and is only created when a new object is created. If this object is destroyed, then the variable is also destroyed.
Class/Static variable: It is declared in static keyword of a class.

Describe Java Modifier types?
A modifier is a keyword that can be added to the definition of a variable, class, or method, so as to change the meaning of the definition. There are two main Java modifier types:
1.Access control modifier – It is used to set access level.
2.Non-access modifier – It serves to achieve a specific functionality, and there are four types; abstract modifier, static modifier, final modifier, and the synchronized modifier.

List the Basic operators in Java

The operators used to manipulate variables in Java can be classified into the following 6 groups:
– Arithmetic Operators
– Assignment Operators
– Bit wise Operators
– Relational Operators
– Logical Operators
– Miscellaneous Operators

What is Java Loop control?
In any block of code, statements are executed in a sequential manner, and this means that the first statement is executed before the second statement. However, a programmer may need a specific statement to be executed several times, before the next block of code is executed. In Java, there exists a control structure that allows for this statement
be executed as many times as necessary before the next statement is executed. This control structure is called loop control, and it ensures that the required statement, which is called a loop statement, is executed multiple times. Usually, the loop statement is part of a conditional code.

Describe decision making in Java

Decision making in Java follows a structured format in which a condition is set using a conditional operator. The program can only proceed once the condition is fulfilled and a set outcome is obtained. Therefore, decision making in Java follows the following format; a condition is set, and a conditional operator is used to construct a conditional code. If codes are executed, they are checked by the conditional code to determine whether they have fulfilled specific stipulations, and if they have not fulfilled them, they are looped back into the code execution path. This ensures that a desired outcome is achieved, and that the program can properly decide if that outcome has been

What are access, specifiers?

Specifiers give you the power to access and regulate the classes and fields and methods in the Java field. They can also restrict the access. It is a restrictive code. Examples of these are:

· Private

· Public

· Protected

· Default

To begin with, public specifiers can get accessed from any class in a java program. You don’t need to follow any class in a java program.Their proximity doesn’t matter, order them from which package you have. An example of these is

Public class Demo {
// public class

Public x, y, size;
// public instance variables


What is polymorphism ?

As we know, this word comes from two root words poly which means comprising of many and morphos which denotes form. However, in Java, it is used to refer to a situation when a parent class or super class references to a child class object, which is a subclass of an object oriented programming. An appropriate example is these is:

classbase_class() {

def bproperty1()

def bproperty2()

def bproperty3()


classderived_class() {

def dproperty1()


baseclass_obj1 =

derivedclass_obj2 = derived_class()

What is a hash set?

Is a class used to create a collection that uses a hash table for storage.These is an example of how you can add an element, remove it from the element or size the hash set.

package com.java2novice.hashset;


public class MyBasicHashSet {

public static void main(String a[]){

HashSeths = new HashSet();

//add elements
to HashSet





System.out.println(“Is HashSet empty?



System.out.println(“Size of the HashSet:

System.out.println(“Does HashSet contains first
element? “+hs.contains(“first”));



– See more at:


Public class MyBasicHashSet {

public static void main(String a[]){

HashSeths = new HashSet();

//add elements
to HashSet



package com.java2novice.hashset;


public class MyBasicHashSet {

public static void main(String a[]


What is a tree set?

It is a class in Java which implements the set interface that uses a tree for storage.

Import java.util.TreeSet;

public class TreeSetExample {

public static void main(String args[]) {

// TreeSet of
String Type

TreeSettset = new TreeSet();

// Adding
elements to TreeSet









// TreeSet of
Integer Type

TreeSet tset2 = new TreeSet();

// Adding
elements to TreeSet










What is an Enumiset?

It is a class in java where the order in which the value of an enum gets initially declared. An example of this is

publicenumWhoisRIR {










private String URL;

WhoisRIR(String Url) {

this.url =


public String url() {




public class Test {

public static void main(String[] args) {