Java Interview Questions and Answers
Image Source - Pixabay

Nowadays, almost all kinds of rich applications in smartphones, tablets, desktops and other IT enabled software uses Java as their platform.  And thus the demand for Java developers and programmers has increased in the market ever than before! When appearing for the interview for Java developers, programmer or tester, the employer will ask you a certain set of questions. In this guide, we have covered almost 50+ important core Java interview questions and answers for Freshers and Experienced Candidates.

Check out this tutorial guide to learn more about Java interview questions and answers as well as interview tips! This guide is prepared by the team of Java experts to help you understand the basic concepts of Java programming. Also, this tutorial covers all of the important Java topics including Java definitions, access specifiers, OOPS concept, threads, exceptions etc. We assure that you will get here nearly the 90% frequently asked interview questions and answers. Go through this Java Interview Questions and Answers guide to make yourself ready to face any Java interview confidently.

Most Popular Java Interview Questions

Below we have come up with the comprehensive list of the most popular Java interview questions along with detailed answers.

Question 1: Define Class in Java?

Ans: In Java, a class is a template used to create objects and define the data type. It acts as a building block for Java language-oriented systems.

Question 2: Difference between Static and Dynamic Loading

Ans: Static class loading involves the creation of objects and instances using new keywords, and dynamic class loading is done when the name of the class is not known at compile time.

Question 3: How Do You Connect to a Database in Java?

Ans: The steps to connect to a database in Java are:

•    Registering the driver class

•    Creating connection

•    Creating statement

•    Executing queries

•    Closing connection

Question 4: Difference Between Overloading and Overriding

Ans: When there are two methods of the same name but different properties, it is overloading. Overriding occurs when there are two methods of the same name and properties, one is in the child class and the other is in the parent class.

Question 5: What’s the Base Class of all Exception Classes?

Ans: Java.lang.Throwable is the superclass of all exception classes, and all exception classes are derived from this base class.

Question 6: State Two Differences Between an Inner Class and a Subclass

Ans: While Inner classes are in the same file, subclasses can be in another file. With that in mind, while subclasses have the methods of their parent class, inner classes get the methods they want.

Question 7: Define JSON.

Ans: JSON is an acronym for JavaScript Object Notation. It uses JavaScript syntax, and the format is text only.

Question 8: What Are the Types of Constructors?

Ans: There are two types of constructors:

1. Default constructor: 

A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, the compiler creates a default constructor (with no arguments) for the class.

2. Parameterized constructor

A constructor that has known parameters is a parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.

Question 9: What Is The Difference Between Equals() And == ?

Ans: Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

Question 10: What Is Runtime Polymorphism Or Dynamic Method Dispatch?

Ans: 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.

Question 11: Can You Override A Private Or Static Method In Java?

Ans: You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it’s not accessible there. What you can do is create another private method with the same name in the child class.

Question 12: What is Request Dispatcher?

Ans: Request Dispatcher interface is used to forward the request to another resource that can be HTML, JSP or another servlet in same application. We can also use this to include the content of another resource to the response.

There are two methods defined in this interface:

1. Void forward()

2. Void include()

Question 13: How Does Cookies Work In Servlets?


  • Cookies are text data sent by server to the client and it gets saved at the client local machine.
  • Servlet API provides cookies support through javax.servlet.http.Cookie class that implements Serializable and Cloneable interfaces.
  • HttpServletRequest getCookies() method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.
  • Similarly HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in response header, there are no getter methods for cookie.
Question 14: What are the different methods of session management in servlets?

Ans: Session is a conversational state between client and server and it can consists of multiple request and response between client and server. Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response.

Some of the common ways of session management in servlets are:

  1. User Authentication
  2. HTML Hidden Field
  3. Cookies
  4. URL Rewriting
  5. Session Management API
Question 15: Explain the creation of a thread-safe singleton in Java using double-checks locking

Ans: Singleton is created with double checked locking as before Java 5 acts as an broker and it’s been possible to have multiple instances of Singleton when multiple threads creates an instance of Singleton at the same time. Java 5 made it easy to create thread-safe Singleton using Enum. Using a volatile variable is essential for the same.

Question 16: Can we execute any code, even before the main method? Explain?

Ans: Yes, We can execute any code, even before the main method. We are using a static block of code in the class when creating the objects at load time of class. Any statements within this static block of code will get executed one time while loading the class, even before the creation of objects in the main method.

Question 17: How can we restrict inheritance for a class?

Ans: We can restrict inheritance for class by following steps.

  1.  By using final keyword
  2.  If we make all method final, then we cannot override that.
  3.  By using private constructors
  4. By using Javadoc comment (//)
Question 18: Are constructors inherited? Can a subclass call the parent’s class constructor?

Ans: We cannot inherit a constructor. We create an instance of a subclass using a constructor of one of its superclass. Because override the superclass constructor is not our wish so that, we override a superclass constructor, then we destroy the encapsulation abilities of the language.

Question 19: Name the methods of Object Class?


clone() – This method helps to create and return a copy of the object.

equals() – This method helps to compare

finalize() – Called by the garbage collector on an object

getClass() – It helps to return the runtime class of an object.

hashCode() – Helps to return a hash code value for the object.

toString() – helps to return a string representation of the object.

notify(), notifyAll(), and wait() – It helps to synchronize the activities of independently running threads in a program.

Question 20: Describe Java Annotations

Ans: Java Annotations is a tag which symbolizes metadata associated with class, interface, methods, fields, etc. Annotations do not directly influence the operations. The additional information carried by annotations are utilized by java compiler and JVM.

Question 21: Java doesn’t use pointers. Why?

Ans: Pointers are susceptible and slight carelessness in their use may result in memory problems and hence Java basically manages their use.

Question 22: Which API is provided by Java for operations on a set of objects?

Ans: Java provides a Collection API which provides many useful methods which can be applied to a set of objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet, and TreeMap.

Question 23: Explain the uses of Synchronized block

Ans: We are using Synchronized block because

  •  It helps to lock an object for every shared resource.
  •  The scope of synchronized block is smaller than the method.
Question 24: What are the functions of hashCode() method?

Ans: This method hashCode() returns a hash code value (an integer number). And also returns the same integer number, if two keys (by calling equals() method) are same. But in sometimes two hash code numbers can have different or same keys.

Question 25: What is Expression Language in JSP?

Ans: The expression language is used in JSP to simplify the accessibility of objects and It provides many objects that can be used directly like param, requestScope, sessionScope, applicationScope, request, session etc.

Question 26: Explain Java virtual machine?

Ans: Java Virtual Machine is a virtual machine that enables the computer to run the Java program. JVM acts like a run-time engine which calls the main method present in the Java code. JVM is the specification which must be implemented in the computer system. The Java code is compiled by JVM to be a Bytecode which is machine independent and close to the native code.

Hope we have got covered most of the frequently asked Java Interview Questions and Answers! In case, if we have missed any important question, kindly let us know in the comment box. For more updates on programming languages, guides, tutorials and interview tips, stay tuned with!


Please enter your comment!
Please enter your name here