Top 30+ Java Interview Questions and Answers

  • By Shital Chauhan
  • March 16, 2024
  • JAVA Programming
Top 30+ Java Interview Questions and Answers

Top 30+ Java Interview Questions and Answers

Explore Top 30+ Java Interview Questions and Answers with SevenMentor’s expert-curated resource to boost your confidence and stand out in Java interviews!

Q1. What is Java?

Java is an object-oriented programming language with flexible real-time implementation. Java allows programmers to write once and run anywhere (WORA), meaning compiled Java code can execute on all Java-supported platforms without recompilation. Java applications are usually compiled to bytecode that works on any JVM, regardless of computer architecture

Q2. What are the features of Java programming language?

  • Java is simple as it uses all basic programming constructs of the C language.
    Example: Variables, control statements like if-else, all loops (for-loop, while-loop, do-while-loop).
  • Java is an object-oriented language which makes it suitable for real-time application development.
  • Java is platform-independent.
  • Java is a portable language
  • Java supports for multithreaded applications
  • Java is the most secure language because of JVM
  • Java offers a secure feature that aids in the development of a virus-free and tamper-proof system for users.
  • Java is a robust language because of its exception handling feature.

 Q3. Why main method is static in Java?

In java main method is static because static methods do not require an object to call them. In java static methods are called by using a class name. So JVM calls the main method by using its class name. If we don’t require the object it will also save the memory which will occupy by that object. Hence static methods are said to be memory efficient.


Q4. Differentiate between the constructors and methods in Java?

Methods Constructors
1. Used to represent the functionality of an object 1. Used to initialize the state of an object
2. Must have a return type 2. Do not have any return type
3. Needs to be invoked explicitly 3. Is invoked implicitly
4. No default method is provided by the compiler 4. A default constructor is provided by the compiler if the class has none
5. Method name may or may not be the same as class name 5. The constructor name must always be the same as the class name
6. Methods can be overridden 6. The constructor cannot be overridden


For Free, Demo classes Call: 020-71173125

Registration Link: Click Here!


Q5. What is final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

  • final variable

When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

  • Final Method

When a method is declared final then it can’t be overridden by the inheriting class.

  • Final Class

When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.

Q6. Differentiate between static and non-static methods in Java.

Static Method Non-Static Method
1. The static keyword must be used before the method name 1. No need to use the static keyword before the method name
2. It is called using the class (className.methodName)  2. It can be called any general method
3. They can’t access any non-static instance variables or methods 3. It can access any static method and any static variable without creating an instance of the class


Q7. Difference between String, StringBuilder, and StringBuffer.

Factor String StringBuilder StringBuffer
Storage Area Constant String Pool Heap Area Heap Area
Mutability Immutable Mutable Mutable
Thread Safety Yes No Yes
Performance Fast More efficient Less efficient

Q8. Why Strings are immutable in Java?

In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, an action from one client might affect the rest. It enhances the security, caching, synchronization, and performance of the application. 

Q9. Why Java is platform independent?

Java is called platform independent because of its JVM which runs byte codes and JVM comes with any Operating system. So JVM and byte code make java platform independent. 

Q10. Is Java 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, and short which are not objects.

Q11. What are constructors in Java?

In Java, constructor are special methods which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are three types of constructors:

  1. Default Constructor: In Java, a default constructor is one that does not take any inputs. This constructor is given by the Java compiler when no constructor is defined in the class. Its main purpose is to initialize the instance variables with the default values. Also, it is mainly used for object creation. 
  2. No-argument Constructor: In Java, a No-argument constructor is similar to a default constructor but it’s defined by the programmer in the class and not implicitly given by the compiler. Its main purpose is to initialize the instance variables with the default values the same as the default constructor.
  3. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.

Java rule says either defined No-argument constructor and Parameterized constructor in the class or none of them. Then in this case to create the object and initialize it with default values default constructor will be provided by the compiler. If we define any one of the No-argument constructors or parameterized constructors in the class the Java compiler will not provide any default constructor for that class.

Q12. What is a singleton class in Java and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

Q13. What is the difference between an Array list and a vector in Java?

ArrayList Vector
Array List is not synchronized.  Vector is synchronized.
Array List is fast as it’s non-synchronized. Vector is slow as it is thread-safe.
If an element is inserted into the Array List, it increases its Array size by 50%. Vector defaults to doubling size of its array.
Array List does not define the increment size. Vector defines the increment size.
Array List can only use Iterator for traversing an Array List. Vector can use both Enumeration and Iterator for traversing.


Q14. What is the difference between equals () and == in Java?

Equals () method is defined in the Object class in Java and used for checking the equality of two objects defined by business logic. The equals method compares the objects based on their values so, it will give a deep comparison. The == operator is called the equality operator it is used to compare the references of the object not their values.

So, the equality operator (==) gives a shallow comparison. Always use the equals method to compare the two objects instead of == operator.


Q15. When can you use the super keyword and this keyword?

The ‘super’ keyword refers to the immediate parent class object. Following are the uses of the super keyword. 

  1. To call the constructor of the parent class in the child class while creating the instance of the child class.
  2. To access the parent class methods and attributes in the child class we can use the super keyword.
  3. To access the immediate parent class methods and attributes.

The ‘this’ keyword is used to refer to the current class object. Following are the uses of the ‘this’ keyword.

  1. To remove the shadowing of the instance variables.
  2. To access current class methods and attributes.
  3. To do the constructor chaining

Q16. What are the differences between HashMap and HashTable in Java? 

HashMap Hashtable
It is nonsynchronized. It cannot be shared between many threads without proper synchronization code. It is synchronized. It is thread-safe and can be shared with many threads.
It permits one null key and multiple null values. It does not permit any null key or value.
is a new class introduced in JDK 1.2. It was present in earlier versions of java as well.
It is faster. It is slower.
It is traversed through the iterator. It is traversed through the Enumerator and Iterator.
It uses fail fast iterator. It uses an enumerator which does not fail fast.
It inherits the AbstractMap class. It inherits Dictionary class.


For Free, Demo classes Call: 020-71173125

Registration Link: Java Training in Pune!


Q17. What is the importance of reflection in Java?

Reflection is a runtime API for inspecting and changing the behavior of methods, classes, and interfaces. Java Reflection is a powerful tool that can be really beneficial. Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time. Reflection can also be used to create new objects, call methods, and get/set field values. External, user-defined classes can be used by creating instances of extensible objects with their fully-qualified names. Debuggers can also use reflection to examine private members of classes.

Q18. How to not allow serialization of attributes of a class in Java?

The non-serialized attribute can be used to prevent member variables from being serialized. We should also make an object that potentially contains security-sensitive data non-serializable if possible. Apply the transient keyword with an attribute in the object that we don’t want to serialized. Then the particular attribute is being non-serialized in the object. 

Q19. What are the main concepts of OOPs in Java?

Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:

  1. Inheritance: Inheritance is a process where one class acquires the properties of another and gives the reusability.
  2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit and provides data security.
  3. Abstraction: Abstraction is the process of selecting only required attributes and methods for creating class and ignoring the rest. Two types of abstraction are there: data and procedural abstraction. In data abstraction we are only selecting required attributes and methods relevant to the domain and ignoring the rest. In procedural abstraction, we are hiding the implementation details from the user and showing only the functionality for reducing the complexity.
  4. Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms. It increases the readability and maintainability of the code.


 Q20. Define a Java Class.

A class in Java is a blueprint that includes all your data.  A class contains fields (variables) and methods to describe the behavior of an object. Let’s have a look at the syntax of a class.

class Abc {

member variables // class body



Q21.What is the difference between collection and collections

The collection is a framework which consists of a collection of classes and interfaces used to manage and manipulates group of objects. Collections is a utility class that provides static methods to be used on all types of collections. Examples of static methods are sort, synchronizedList(), and synchronized collection.

Q22. What is a map in Java?

In Java, Map is an interface of the Util package that maps unique keys to values. The Map interface is not a subset of the main Collection interface and thus it behaves a little differently from the other collection types. Below are a few of the characteristics of the Map interface: 

  1. The map doesn’t contain duplicate keys.
  2. Each key can map at max one value.

Q23. What is polymorphism in Java?

Polymorphism is one of the important pillars of object-oriented language. Poly means many and morphs mean forms so it represents one thing that can have many forms. In Java we can define methods with the same name but different forms. There are two types of polymorphism 

  1. Compile time polymorphism (Method overloading)
  2. Run time polymorphism (Method overriding)

Q24. What is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. Let’s take a look at the example below to understand it better.

class Car {

void run()


System.out.println(“Car is running”); 



class Audi extends Car {

void run()


System.out.prinltn(“Audi is running safely at 100km/h”);


public static void main(String args[])


Car b= new Audi();    //upcasting;

Q25. What is an abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus, you can say that Abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways: 

  1. Abstract Classes (0-100% of abstraction can be achieved)
  2. Interfaces (100% of abstraction can be achieved)

Q26. What do you mean by an Interface in Java?

An interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Thus, the interface basically is a group of related methods with empty bodies. Example:

public interface Animal {

 public void eat();

 public void sleep();

 public void run();


Q27. What is the difference between abstract classes and interfaces?

Abstract Class Interfaces
An abstract class can provide abstract methods and concrete methods An interface cannot provide any concrete methods at all, just the abstract methods
In the case of an abstract class, a class may extend only one abstract class A Class may implement several interfaces
An abstract class can have non-abstract methods All methods of an Interface are abstract
An abstract class can have instance variables An Interface cannot have instance variables
An abstract class can have any visibility: public, private, protected An Interface visibility must be public (or) none
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method
An abstract class can contain constructors An Interface cannot contain constructors
Abstract classes are fast Interfaces are slow as it requires extra indirection to find the corresponding method in the actual class

Q28. What is an association in Java?

The association is a relationship where all object have their own lifecycle and there is no owner. Let’s take the example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationships can be one-to-one, one-to-many, many-to-one, and many-to-many.

Q29. What do you mean by aggregation?

An aggregation is a specialized form of Association where all object has their own lifecycle but there is ownership and child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher’s object will not be destroyed. 

Q30. What is a composition in java?

Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of a relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different houses if we delete the house room will automatically delete.

Q31. What is a marker interface?

In Java, a marker interface is an interface that does not declare any methods or fields. Its sole purpose is to mark or tag a class as having a certain characteristic or capability. By implementing a marker interface, a class indicates that it possesses specific behavior or qualifies for a particular treatment. Example of marker interfaces are Serializable, Cloneable etc.

Q32. Why multiple inheritance is not supported in Java?

The problem with multiple inheritance is that if multiple parent classes have the same method name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class.

Therefore, Java doesn’t support multiple inheritance. The problem is commonly referred to as the Diamond Problem.

Q33. What are Java8 features?

Java 8 features are used to support the functional programming in Java. Following are the important features of java 8.

  1. Lambda expressions
  2. Functional interfaces
  3. Method references
  4. Default and static methods
  5. Date and Time API
  6. Streams API for handling the collections

Q34. What is lambda expression?

Lambda expressions in Java are anonymous methods used to give the implementation of functional interfaces. It’s a block of code which can take the parameters and also returns a value. It reduces the code.

Q 35. What are functional interfaces? 

Functional interfaces are the interfaces which consist of only one abstract method and any number of static and default methods. Some of the built-in functional interfaces are 

Function, Consumer, Predicate, and Supplier. functional interfaces are used to manipulate the functions like any other functional programming language. With help of functional interfaces, we can pass a function as argument to another function and also can returns the function as a variable.

Q36. What is stream API?

Streams API is a collection of classes and interfaces used to manipulate the collections efficiently. Streams API has given many methods used to process the

Collection of objects. A stream API in Java is a sequence of pipelined methods used to process the collection data and produce the desired result. Some examples of Stream API methods are filter, map, sorted, for-each, etc.  Streams API makes handling of collections easy. It gives code reusability and help to reduce the code.

Note: Do watch our latest video: Click Here



Shital Chauhan
Call the Trainer and Book your free demo class for JAVA now!!!

© Copyright 2020 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *