Learn to Code via Tutorials on Repl.it!

← Back to all posts
Java interview questions and answers — 2021

Here are some basic java interview questions and answers.

What is Java Development Kit(JDK)?
A) JDK is a software development kit which includes tools and libraries necessary for developing java applications.

What does Java Runtime Environment(JRE) do?
JRE refers to runtime environment where java bytecode is executed. JRE supports files and libraries for a runtime environment.

What does Java Virtual Machine(JVM) do?
JVM is an abstract machine which provides runtime environment in which java bytecode can be executed. Learn preface to java virtual machine and architecture.

Java is platform independent. Why?
In any other programming language source code is compiled into executable code and this code cannot be run across all platforms.
Using JVM we can make byte code understandable to any platform and this bytecode is platform-independent.
Meanwhile JVM is different for each platform and java is platform-independent because it does not depend on any type of platform. Hence, java is platform-independent.

Java is not 100% object-oriented. Why?
Because java uses eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

What are the constructors in java?
Constructor is a block of code used to initialize objects.

For example:


What does singleton class do?
singleton class can have only one object (an instance of the class) at a time. After object is created and if we try to instantiate singleton class, new variable also points to first object created.

What are wrapper classes in java?
wrapper classes allow primitive data types a way to convert into object and vice-versa.

For example:
int a = 7; // using primitive datatype
Integer a = new Integer(7); // using wrapper class

What is the difference between == operator and equals method in java?
In general == is an operator and equals() is a method. We use == operator for reference comparison of objects in heap.
Because in java we don’t have concept of operator overloading. Meanwhile == operator is used for checking address of two objects is same or not.
That is, == operator checks if both objects point to the same memory location or not.
Also == operator is used to compare object types and primitive types like boolean. While equals() method of String class compares the content of two objects.

What are the OOP’s concepts in java?*
Abstraction is defined as hiding internal implementation and showing only necessary information.
Inheritance is a procedure of acquiring all the properties and behaviour of a parent class (super class) into child class (subclass).
encapsulation is a procedure of binding data or variables and methods together.
Polymorphism literally means many forms. Polymorphism is the ability of a method to perform different tasks. It can be achieved through methods.

Why main() method is always static in java?
Because to call static method object is not required. If main() were to be a non-static method Java Virtual Machine have to create its object first and then call main() method which will lead to the extra memory allocation.

Why strings are immutable in java?
Strings are immutable in java because String objects are cached in string constant pool.
Learn strings and String constant pool here.

What are Arrays and ArrayList in java?
Array is an object which holds fixed number of elements of similar type.
1. ArrayList is part of collection framework.
2. Arraylist implements list interface.
3. ArrayList is a re-sizable array that grows dynamically when elements are added and decreased when elements are deleted.
4. For frequent retrieving operation java ArrayList is the best. Because ArrayList elements are stored in consecutive memory locations.
5. ArrayList cannot hold primitive data types such as int, double, char, and long.
6. ArrayList can hold String and wrapper class objects (Double, Integer).
7. ArrayList allows duplicate elements.
8. ArrayList preserves insertion order.
9. ArrayList is widely used because of its functionality and flexibility. It is designed to hold heterogeneous collections of objects.
10. ArrayList can have any number of null values.

Difference between hashset and hashmap in java?
In HashSet,
1. We can store objects in HashSet. For example HashSet:{“Hello”, “World”}
2. insertion order is not preserved. It is based on hashcode.
3. has add() method.
4. implements Set interface.
5. do not allow duplicate elements.
6. allows single null value.

In HashMap,
1. In HashMap we can store key and value pairs. For example {1 ->”Hello”, 2 ->”World”}
2. does not maintain insertion order. It is based on Hash function.
3. has put() method.
4. implements Map interface.
5. allows duplicate values. Does not allow duplicate keys.
6. allows single null key and any number of null values.

Difference between this and super keyword in java?
1. this keyword is the reference variable that refers to the current object.
2. this keyword can be used to invoke current class method implicitly.
3. this() keyword used to invoke current class constructor.
4. this keyword can be used to pass as an argument in method call.

super keyword in java
1. super keyword is the reference variable which is used to refer immediate parent class object.
2. super keyword can be used to invoke immediate parent class method.
3. super keyword can be used to invoke immediate parent class constructor.
4. super keyword used to access methods of the base class.

What is break and continue statement in java?
break statement encountered in a loop, that loop will terminate and the control will shift to next statement of the same loop.


continue statement jumps to next iteration of a loop based on specific condition.



What are access modifiers in java?
Access modifiers define the limit or scope of a variable, constructor, class or a method. There are four types of access modifiers in java; private access modifier, protected access modifier, public access modifier and default access modifier.

Difference between for and for each loop in java?
for loop,
1. There is no sequence in execution. Here in for loop we can change counter as per our wish.
2. was introduced from start, JDK 1.
3. no need of implementing interface.
4. can have access to index. Hence can replace element in an array.
5. counter can increment and decrement.

for each loop,
1. Executes in sequence. Counter is increased by one.
2. was introduced from JDK 5 onwards.
3. To loop over containers using for each loop, container should implement Iterable interface.
4. can’t replace element at given index since there is no access to array index.
5. we can only iterate in incremental order cannot decrement.

Example on difference between foreach and for loop.

using for loop:
using for each loop:


Quick tip you don't need to make single line code you can use multi-line markdown codeblocks:

// Java code here

Which looks something like: