DEV Community

loading...

Java interview questions and answers — 2021

LohithS
I'm a software engineer and tech blogger.
・6 min read

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.
Syntax:
code
class DemoClass
{
// constructor name is same as class name
DemoClass()
{
....
}
}
// calls DemoClass() constructor
DemoClass obj = new DemoClass();

For example:
code
public class ConstructorDemo
{
int a; // class attribute
// create constructor for class ConstructorDemo
ConstructorDemo()
{
a = 26; // initial value for class attribute 'a'
}
public static void main(String[] args)
{
// creating object for ConstructorDemo class
// here we're calling constructor ConstructorDemo()
ConstructorDemo obj = new ConstructorDemo();
System.out.println(obj.a);
}
}

Output:
26

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.

ArrayList is part of collection framework.
• Arraylist implements list interface.
• ArrayList is a re-sizable array that grows dynamically when elements are added and decreased when elements are deleted.
• For frequent retrieving operation java ArrayList is the best. Because ArrayList elements are stored in consecutive memory locations.
• ArrayList cannot hold primitive data types such as int, double, char, and long.
• ArrayList can hold String and wrapper class objects (Double, Integer).
• ArrayList allows duplicate elements.
• ArrayList preserves insertion order.
• ArrayList is widely used because of its functionality and flexibility. It is designed to hold heterogeneous collections of objects.
• 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. ________________________________________
  5. super keyword is the reference variable which is used to refer immediate parent class object.
  6. super keyword can be used to invoke immediate parent class method.
  7. super keyword can be used to invoke immediate parent class constructor.
  8. 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.
Example:
code
public class Example
{
public static void main(String[] args)
{
for(int a = 1; a <= 10; a++)
{
if(a == 3)
{

// breaking loop

break;

}

System.out.println(a);

}
}
}

Output:
1
2

continue statement jumps to next iteration of a loop based on specific condition.
Example:
code
public class Example
{
public static void main(String[] args)
{
for(int a = 1; a <= 10; a++)
{
if(a % 2 != 0)
{
continue;
}
System.out.println(a + " ");
}
}
}

Output:
2
4
6
8
10

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.

code
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DifferenceBetween
{
public static void main(String[] args)
{
List players = new ArrayList<>(Arrays.asList("Virat", "Rohit", "Dhoni"));
// iterate over List using for loop
System.out.println("using for loop: ");
for(int a = 0; a < players.size(); a++)
{
System.out.println(players.get(a));
}
// iterate over List using enhanced for loop
System.out.println("using for each loop: ");
for(String str : players)
{
System.out.println(str);
}
}
}

Output:
using for loop:
Virat
Rohit
Dhoni
using for each loop:
Virat
Rohit
Dhoni

Discussion (0)