Java is one of the most widely used programming language and in this post, I have provided java interview questions with answers which are widely asked from Java developers (Beginners or professional developers).

java-interview-questions-min.png

What is Java?

Java is an object-oriented computer language developed by James Gosling in 1995. Java is fast, secure and platform-independent programming langauge which can be used to create mobile-apps, games, web-applications etc.

What do you mean by Object in Java?

Java is an object oriented programming languages, in which we create classes which contains methods, variables etc. An object has a state and behavior.

A Java object is a combination of data and procedures working on the available data. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes.

What is the difference between StringBuffer and StringBuilder in Java?

The difference between StringBuffer and StringBuilder is StringBuffer is thread-safe, that is StringBuffer is synchronized, while StringBuilder is non-synchronized.

Also, StringBuffer performance is slower than StringBuilder.

What is difference between JDK, JRE, and JVM?

difference-between-jre-jdk-jvm-min.png

Java Virtual Machine (JVM): It is an abstract computing machine that enables a computer to run a Java program. An instance of a JVM is an implementation running in a process that executes a computer program compiled into Java bytecode. JVM performs tasks like loading byte code, code verification, code execution, etc.

Java Runtime Environment (JRE):It is a software package (a physical entity) that contains necessary artifacts required to run a Java program. It includes JVM implementation together with an implementation of Java Class Library (rt.jar). Hotspot is the JVM implementation for Oracle Java.

Java Development kit (JDK): The JDK is a super set of the JRE, and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.

What is constructor in Java?

The constructor is a method which has the same name as the class name.When a new object is created in a program a constructor gets invoked corresponding to the class.The constructor can be overloaded.

What is Class in Java?

Java uses OOP concept hence all code is written in Class, which has variables and methods.

To use those variables and methods, we create class's object, using object we call variables and methods of the class.

Methods are the place where the exact business logic has to be done. It contains a set of statements (or) instructions to satisfy the particular requirement.

What are OOPs Concepts in Java?

  • Inheritance: Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class. The existing class is known as the Super class whereas the derived class is known as a sub class.
    Super class:
    public class AddBase(){
    }
    Sub class:
    public class Addition extends AddBase(){
    }?
  • Encapsulation: Binding (or wrapping) of data and function in a single unit is called encapsulation. The encapsulation technique is used to achieve abstraction in oops.
    public class Addition(){
    private int a = 5; //Here the variable is marked as private, so it cannot be changed by anyone else, outside class 'Addition'
    }?
  • Polymorphism: Polymorphism (more-form) means the ability to take more than one form. It’s used when one task needs to perform in different ways.For Example, areas of shape are having different formulae for each same like rectangle, square, circle, etc.
            void run()
    	{
    	}
    	void run(int x)
    	{
    	}?
  • Abstraction : Abstraction is a concept to show only essential detail i.e hide internal detail and show functionality in simple terms. To achieve abstraction in Java, use abstract class or interface.

What is meant by Method Overriding?

Method Overriding means if the sub-class method name is the same Super-class method and also, it has same arguments and return type.

Example:

public class MainClass{ //Super class
public void add(){
  //do something
}
}
 
Public class SecondClass extends MainClass(){
Public void add(){
//do something
}
Public static void main(String args[]){
MainClass addition = new SecondClass (); //Polymorphism is applied
addition.add(); // It calls the Sub class add() method
}
}

What is method overloading in Java?

Method overloading is mainly used in a situation when we want to create multiple specialized versions of a method in a class.

  • Multiple versions of a method can be created in the same class.
  • Multiple versions of a method can be created in a subclass, through inheritance.

Example:

class Math
{
 public void add(int a, int b)		//add method adding two integers
 {
 System.out.println(a+b);
 }


 public void add(float a, float b)	//add method adding two floats
 {
 System.out.println(a+b);
 }
}

class Overload
{
 public static void main()
 {
 Math ob = new Math();
 ob.add(1,2);
 ob.add(2.5f,4.5f); //float based method is called, hence overloading
 }
}

What is Abstract class in Java?

An abstract class is declared with the abstract keyword. Unlike a regular java class, an abstract class may not only contain the regular methods, defined with curly braces { } but may also contain the abstract methods, ending with a semicolon; or a mix of regular and abstract methods.

Example:

abstract class A
{
 abstract public void m();
 public void m2()
 {
  System.out.println("Concrete method of abstract class");
 }
}

What is difference between Array and Array List?

  • In Array size should be given at the time of array declaration while in ArrayList Size may not be required, as It can change the size dynamically.
  • In Array, to put an object into array we need to specify the index while in ArrayList Index is not required to add new element.
  • Array is not type parameterized while ArrayList is parameterized.

What is difference between Default and Protected access specifiers?

Default: Methods and variables declared in a class without any access specifiers are called default.

For example, There are three classes, Class A, Class B and Class C. Class A and Class B are in same package while class C in another package.

Default members in Class A are visible to the other classes which are inside the package and invisible to the classes which are outside the package.

So Class A members are visible to the Class B and invisible to the Class C.

Protected: Protected is the same as Default but if a class extends then it is visible even if it is outside the package.

Considering above class examples, Class A members are visible to Class B because it is inside the package. For Class C it is invisible but if Class C extends Class A then the members are visible to the Class C even if it is outside the package.

What is difference between HashMap and HashTable?

  • Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
  • Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  • Iterator in the HashMap is fail-fast and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.
  • Data retrieval is slower than dictionary because of boxing-unboxing in HashTable, while HashMap Performance is high than HashTable.

What is difference between Abstract class and Interface?

  • Abstract classes have a default constructor and it is called whenever the concrete subclass is instantiated while Interface doesn’t have any constructor and couldn’t be instantiated.
  • Abstract classes contains Abstract methods as well as Non-Abstract methods while in Interface, only abstract methods are declared.3
  • The class which extends the Abstract class shouldn’t require the implementation of all the methods, only Abstract methods need to be implemented in the concrete sub-class while Classes that implement the interface should provide the implementation for all the methods.