top of page

Java Interview Questions & Answers

Question: What is Java?


Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

Question: Mention some features of Java?


Some of the features which play important role in the popularity of java are as follows:

a) Simple: Java is easy to learn. Eventhough Java is based on C++ , it was developed by eliminating poor programming practices of C++.

b) Object-Oriented: Java is a object oriented programming language. Everything in Java is an Object.

c) Portable: Java run time environment uses a bytecode verification process to make sure that code loaded over the network doesn’t violate Java security constraints.

d) Platform independent: Java is platform independent. Java is a write once, run anywhere language. Without any modifications, we can use a program in different platforms.

e) Secured: Java is well known for its security. It delivers virus free systems.

High Performance: Java enables high performance with the use of JIT (Just-In-Time) compilers

f) Multithreaded: Java Multithreaded features allows us to write programs that can perform many tasks simulatenously. Multithreading concept of Java shares a common memory area. It doesn’t occupy memory for each thread.

Question: What is the difference between Declaration and Definition in Java?


Declaration: If you just declare a class or method/function or variable without mentioning anything about what that class or method/function or variable looks like is called as declaration in Java.

Definition: If you define how a class or method/function or variable is implemented then it is called definition in Java.

When we create an interface or abstract class, we simply declare a method/function but not define it.

Question: What is an Object in Java?


An object is an instance of a class. Objects have state (variables) and behavior (methods).

Example: A dog is an object of Animal class. The dog has its states such as color, name, breed known as variables, and behaviors such as barking, eating, wagging her tail.


Question: What is a Class in Java?


A class can be defined as a collection of objects. It is the blueprint or template that describes the state and behavior of an object.


Question: What is Constructor in Java?


Constructor in Java is used in the creation of an Object that is an instance of a Class. Constructor name should be same as class name. It looks like a method but its not a method. It wont return any value. We have seen that methods may return a value. If there is no constructor in a class, then compiler automatically creates a default constructor.

Question: What is Local Variable, Instance Variable & Class variable in Java?


Local Variable:

Local variable is a variable which we declare inside a Method. A method will often store its temporary state in local variables.

Instance Variable (Non-static):

Instance variable is a variable which is declared inside a Class but outside a Method. We don’t declare this variable as Static because these variables are non-static variables.

Class Variable (Static):

Class variable is a variable which is declared as Static. Additionally, the keyword final could be added to include that the value will never change.


Question: What are the OOPs concepts?


OOPS Stands for Object Oriented Programming System. It includes Abstraction, Encapsulation, Inheritance, Polymorphism, Interface etc.,

Question: What is Inheritance in Java?


Inheritance is a process where one class inherits the properties of another class

Question: What is Polymorphism?


Polymorphism allows us to perform a task in multiple ways. Let’s break the word Polymorphism and see it, ‘Poly’ means ‘Many’ and ‘Morphos’ means ‘Shapes’

In picture below, Same Gentleman can take different roles like Father, Employee, Shopper as per need etc.

Question: What are the types of Polymorphism?


There are two types of Polymorphism in Java

1. Compile time polymorphism (Static binding) – Method overloading

2. Runtime polymorphism (Dynamic binding) – Method overriding

We can perform polymorphism by ‘Method Overloading’ and ‘Method Overriding’

Question: What is Method Overloading?


A class having multiple methods with same name but different parameters is called Method Overloading. There are three ways to overload a method.

a) Parameters with different data types

b) Parameters with different sequence of a data types

c) Different number of parameters

Question: What is Method Overriding?


Declaring a method in child class which is already present in the parent class is called Method Overriding.

In simple words, overriding means to override the functionality of an existing method.

In this case, if we call the method with child class object, then the child class method is called. To call the parent class method we have to use super keyword.

Question: What is Abstraction in Java?


Abstraction is the methodology of hiding the implementation of internal details and showing the functionality to the users.

Abstraction In Java

Example: Mobile Phone.

A layman who is using mobile phone doesn’t know how it works internally but he can make phone calls.

Question. What is Abstract Class in Java?


We can easily identify whether a class is an abstract class or not. A class which contains abstract keyword in its declaration then it is an Abstract Class.


abstract class <class-name>{}

Points to remember:

a) Abstract classes may or may not include abstract methods

b) If a class is declared abstract then it cannot be instantiated.

c) If a class has abstract method then we have to declare the class as abstract class

d) When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Question: What is Abstract Method?


An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void myMethod();

In order to use an abstract method, you need to override that method in sub class.

Question: What is Interface in Java?


An interface in Java looks similar to a class but both the interface and class are two different concepts. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract. We can achieve 100% abstraction and multiple inheritance in Java with Interface.

Question: What is Encapsulation in Java?


Encapsulation is a mechanism of binding code and data together in a single unit. Let’s take an example of Capsule. Different powdered or liquid medicines are encapsulated inside a capsule. Likewise in encapsulation, all the methods and variables are wrapped together in a single class.

Second View:

Question: Write a program to print the pattern given below


1 2

1 2 3

1 2 3 4

1 2 3 4 5


Question: Write a program to print Fibonacci Series up to count 10.


Question: How to reverse a String in Java?


Second Approach:

Question: How To Find The Largest Value From The Given Array.


Question: How to display all the prime numbers between 1 and 100


The number which is only divisible by 1 and itself is known as a prime number. For example 2, 3, 5, 7, 11… are prime numbers.

Question: How to display all the prime numbers between 1 and n (n is the number, get the input from user)


Question: How to find the given number is a prime number or not by getting input from the user ?


Question: Write a program to print Fibonacci Series?


Method 1:

Method 2:

Question: Difference between Array and ArrayList?



  • Array is static

  • Size of the array should be given at the time of array declaration. We cannot change the size of array after creating it

  • Array can contain both primitive data types as well as objects

  • Arrays are multidimensional


  • ArrayList is dynamic

  • Size of the array may not be required. It changest the size dynamically. Capacity of ArrayList increases automatically whenever we add elements to an ArrayList

  • ArrayList cannot contain primitive data types. It contains only objects

  • ArrayList is always single dimension

Question: Difference between ArrayList and HashSet in Java?



  • ArrayList implements List interface

  • ArrayList allows duplicates

  • ArrayList is an ordered collection and maintains insertion order of elements

  • ArrayList is backed by an Array

  • ArrayList is an index based

  • In ArrayList, we can retrive object by calling get() method or remove object by calling remove() method


  • HashSet implements Set interface

  • HashSet doesn’t allow duplicates

  • HashSet is an unordered collection and doesn’t maintain insertion order

  • HashSet is backed by an HashMap instance

  • HashSet is object based

  • In HashSet, we can’t achieve get() method

Question: What are the different access modifiers available in Java?


Access modifiers are subdivided into four types such as Default, Public, Private, Protected

a) default: The scope of default access modifier is limited to the package only. If we do not mention any access modifier, then it acts like a default access modifier.

b) private: The scope of private access modifier is only within the classes.

Note: Class or Interface cannot be declared as private

c) protected: The scope of protected access modifier is within a package and also outside the package through inheritance only.

Note: Class cannot be declared as protected

d) public: The scope of public access modifier is everywhere. It has no restrictions. Data members, methods and classes that declared public can be accessed from anywhere.

Question: Difference between static binding and dynamic binding?


1. Static binding is also known as early binding whereas dynamic binding is also known as late binding.

2. Determining the type of an object at compile time is Static binding whereas determining the type of an object at run time is dynamic binding

3. Java uses static binding for overloaded methods and dynamic binding for overridden methods.

Question: Difference between Abstract Class and Interface?



  • To declare Abstract class we have to use abstract keyword

  • In an Abstract class keyword abstract is mandatory to declare a method as an abstract

  • An abstract class contains both abstract methods and concrete methods(method with body)

  • Compiler treats all the methods as abstract by default

  • An abstract class provides partial abstraction

  • An abstract class can have public and protected abstract methods

  • An abstract class can have static, final or static final variables with any access modifiers

  • An abstract class can extend one class or one abstract class

  • Abstract class doesn't support multiple inheritance


  • To declare Interface we have to use interface keyword

  • In an Interface keyword abstract is optional to declare a method as an abstract.

  • An interface can have only abstract methods

  • An interface provides fully abstraction

  • An interface can have only public abstract methods

  • An interface can have only public static final variables

  • An interface can extend any number of interfaces

  • Interface supports multiple inheritance

Question: What is Multiple Inheritance?


If a class implements multiple interfaces, or an interface extends multiple interfaces then it is known as multiple inheritance.

Question: How to create singleton class in java?


1. Declare a private constructor to prevent others from instantiating the class.

2. Create the instance of the class either during class loading in a static field/block, or on-demand in a static method that first checks whether the instance exists or not and creates a new one only if it doesn’t exist.


Eagerly Initialized Singleton : Class loading in a static field

Eagerly Initialized Static Block Singleton: Class loading in a static block

Lazily Initialized Singleton: On-demand in a static method


bottom of page