Top 12 Very Common Java Programming Interview Questions/topics 2019!

Java has been here since a very long time and its going to be here for a while. Most of the companies chose Java as primary language for their product development.

Here I have compiled most common interview questions on java. When you attend your next technical interview on java then you might most likely to be asked at least 4 out of below 12 questions/topics.

1. What is JVM, explain its Architecture?

JVM (Java Virtual Machine) is the core of Java programming language, it is where the java byte code actually runs.

For ex. If you write below code and compile with Java compiler It compiles the Java file into a .class file, then that .class file is input into the JVM, which Loads and executes the class file.

 class HelloWorld {
         public static void main(String[] args) {
             System.out.println("Hello, World!"); 

Below is the Architecture diagram of the JVM.

Image credits Internet

Note: Everyone who wants to come into Java world should have a rough idea on each component of above diagram.

2. What is Garbage Collection in Java why it is needed?

The garbage collector is a module/function (as shown in above diagram) which runs on the Java Virtual Machine. It gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.

When a typical Java application is running, it is creating new objects, such as Strings and Files, but after a certain time, those objects are not used anymore. For example, take a look at the following code:

for (File f : files) {
   String s = f.getName();

In the above code, the String s is being created on each iteration of the for loop. This means that in every iteration, a little bit of memory is being allocated to make a String object. Going back to the code, we can see that once a single iteration is executed, in the next iteration, the String object that was created in the previous iteration is not being used anymore — that object is now considered “garbage”.

Automatic memory management schemes like garbage collection makes it so the programmer does not have to worry so much about memory management issues unlike C and C++, So the developer can focus more on application development without worrying about memory leaks.

3. What are SOLID Principles?

SOLID is abbreviation of 5 principles, which helps Java developers to write easily maintainable, bug free code.

SOLID consist of the following principles:

  1. S – Single responsibility : As the name suggests every class should have a single responsibility. For ex. If you write a Class to read a file then it should only read a file, it should not have other functionalities like creating or deleting file etc.
  2. O – Open-closed : Any class or function should allow developers to add new changes without disturbing much of the existing code.
  3. L – Liskov substitution: When you substitute in a new class, make sure it can handle everything that’s thrown at it, and deliver everything expected from it.
  4. I – Interface segregation: There should be enough Interfaces should designed as per the number of functionalities(features) instead one general-purpose interface.
  5. D – Dependency inversion: When dependency needed then it should depend on the abstractions (Abstract Classes/Interfaces) Instead of actual concrete implementations of the functionality.

When the management of a software is done badly, the code can become rigid and more brittle, small changes in the software can result into bugs, to avoid this we follow Solid Principles.

4. Explain Inheritance?

Inheritance is one of the interesting and important feature in Object Oriented languages like java.

When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

5. Difference between Overriding ,Overloading ?

Overloading occurs when two or more methods in one class have the same method name but different set of parameters.

Overriding means having two methods with the same method signature. One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its super class.

6. Comparator, Comparable Interfaces

Comparable interface is used when we want to compare objects using one of their property. It is considered a natural sorting of objects. Consider a list of employees and we want them to be sorted by name as a default sorting order. The comparable interface has compareTo() method which the target class has to implement.

class Employee implements Comparable {
   String name;
   public int compareTo(Employee o) {        
      return name.compareTo(;

Comparator interface provides a compare() method to compare two objects of the same class. Here it provides the flexibility as we can create n number of comparators for each property of an object which we want to compare. Consider the following examples:

class HeightComparator implements Comparator<Employee> {
   public int compare(Employee o1, Employee o2) {
      if (o1.getHeight() < o2.getHeight()) {
         return -1;        
      }else if (o1.getHeight() > o2.getHeight()) {          
         return 1;
      } else {
         return 0;        

Pass the above comparator to sort() method as below and it’ll sort the employeesList accordingly based on height property.

Collections.sort(employeesList, new HeightComparator());
7. HashMap Vs HashTable

Hashtable and HashMap are data-structure based upon hashing and implementation of Map interface, the main difference between them is that HashMap is not thread-safe but Hashtable is thread-safe.

Which means you cannot use HashMap in multi-threaded Java application without external synchronization. Another difference is HashMap allows one null key and null values but Hashtable doesn’t allow null key or values.

8. What is data encapsulation and what is its significance

Encapsulation is a mechanism of binding code and data together in a single unit. In encapsulation, all the methods and variables are wrapped together in a single class.

If you written any single java class so far, there is 100% chance that you already implemented encapsulation in java. In the below example we have encapsulated/enclosed both properties and methods in single block called Student class.

public class Student {
// properties
 private int age;
 private String name;
// methods
 public int getAge(){
 return age;
 public String getName(){
 return name;
 public void setAge(int inputAge){
 age = inputAge;
 public void setName(String inputName){
 name = inputName;
9. What is a singleton class?

Singleton class means you can create only one object/instance for the given class in whole JVM process. You can create a singleton class by making its constructor as private, so that you can restrict the creation of the object. Provide a static method to get instance of the object, wherein you can handle the object creation inside the class only. In this example we are creating object by using static block.

public class SingletonDemo {
    private static SingletonDemo myObj;
        myObj = new SingletonDemo();
    private SingletonDemo(){}
    public static SingletonDemo getInstance(){
        return myObj;
    public void sayHello(){

    // Driver class
    public static void main(String a[]){
        SingletonDemo sd = getInstance();

In the above example we did not create object with new keyword but we are trying to get reference of already existing object myObj by calling getInstance() method.

And if we try to create Object of SignletonDemo class with new keyword we’ll get compilation error, because the constructor is not accessible (it is private).

10. Mutable vs Immutable Objects?

Immutable Object status cannot be changed once instantiated.

A typical Java immutable object must have all its fields be internal, private finalfields. It must not implement any setters. It needs a constructor that takes a value for every single field.

Immutable objects come in handy in multi-threaded environments and in streams. It is great to rely on objects not changing mid-stream. Bugs caused by a thread changing another thread’s object are often subtle and are very, very hard to track down. Immutable objects stop these whole class of problems in their tracks.

Mutable Object is opposite of immutable object, i.e a mutable object can be changed even after its instantiated. Ex. Student object, where you can change any property later once object is created.

11. Explain Multi-threading?

A thread is a lightweight sub-process, the smallest unit of processing. threads use a shared memory area and don’t allocate separate memory area unlike processes, hence saves memory, and context-switching between the threads takes less time than process.

Creating threads in java is lot simpler than any other programming languages hence implementing multi-threading in java applications is common as it gives applications performance boost, and after all it can be achievable with simple syntax.

The process of executing multiple threads simultaneously is known as multi-threading.

12. Exception Handling

An exception is a problem that arises during the execution of a program. It can occur for various reasons say-

  • A user supplied an invalid data
  • Application looking for a file but which is not available
  • Application is trying to connect to Database but Database is down.
  • The JVM has run out of a memory

if an exception is not handled, it may lead to a system failure. That is why exception handling is very important.