Top 25 Java Interview Questions and Answers
Want to be sure to ace your next Java interview? Check out this post to learn more about common Java interview questions, as well as interview tips!
Join the DZone community and get the full member experience.Join For Free
Being the most widely used and deployed language, Java is one of the three core technologies in the world of web. It was created by James Gosling, Patrick Naughton, and Mike Sheridan in 1991. This high-level language has made its mark in the IT industry with its first-class functions, offering a bright future for developers.
Why Is it Important?
In Java, the primary advantage is that it enables developers to write code that runs everywhere (WORM); meaning there isn't a need for recompilation. Regardless of an architecture of a system, Java compiled code can be run on any JVM (Java Virtual Machine), making it one of the most widely used computer programming language. It is class based and object-oriented.
Phases of the Interview:
Here are the three major phases of the interview process:
Phase 1: Invitation
When you apply for jobs at the organizations you aspire to work with, the first step from the employer’s side is reviewal. After reviewing your resume, if you appear to be a good fit for the position, you receive an invitation for the interview. The invitation can be sent in various forms, like e-mail, phone call, message, or even a letter. However, each of them is kept professional and the same is expected from the candidate. Be sure to revert to the invitation professionally and promptly.
Phase 2: Meeting
The next phase is the first round of face-to-face interviews. The main aim of this is to estimate the capabilities of the candidate and understand their personality. It is essential to make a good first impression on the employer by being punctual and professional. Highlight your strengths and very politely, accept your weaknesses. Be confident and positive, but don’t over-do it.
Phase 3: Follow-Up
The last and — certainly not — the least important phase is the follow-up. This phase can either make or break the opportunity. It is essential to adopt a professional way of communication for a follow-up. You can either drop an email, appreciating the time provided for interviewing you, expressing gratitude for the opportunity, or simply giving them a phone call thanking and enquiring about the job.
Most Common Interview Questions
1. Define Class in Java.
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.
2. What Is the Difference Between Static and Dynamic Loading?
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.
3. What Is Multi-Threading?
Multi-threading is a programming concept used to run multiple tasks in a concurrent manner within a single program.
4. When and by Whom was Java Developed?
Java was developed by James Gosling in Sun Microsystem in 1995.
5. What Do JDK, JRE, and JVM Stand for?
• JVM stands for Java Virtual Machine
• JRE stands for Java Runtime Environment
• JDK stands for Java Development Kit
6. Does Java Use Pointers?
No. Java doesn’t use pointers. It has a tough security. Instead of pointers, references are used in Java as they are safer and more secure when compared to a pointer.
7. How Do You Connect to a Database in Java?
The steps to connect to a database in Java are:
• Registering the driver class
• Creating connection
• Creating statement
• Executing queries
• Closing connection
8. What Are the Functions of JVM and JRE?
JVM provides a runtime environment for Java Byte Codes to be executed. JRE includes sets of files required by JVM during runtime.
9. What Is the Difference Between Overloading and Overriding?
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. Check out this post for a more in-depth analysis.
10. What Is the Default Size of the Load Factor in Hashing a Based Collection?
The default size is 0.75, and the default capacity is computed as:
Initial capacity * Load factor
11. What Is a Package?
A package is a collection of related classes and interfaces.
12. What's the Base Class of all Exception Classes?
Java.lang.Throwable is the superclass of all exception classes, and all exception classes are derived from this base class.
13. What Is the Difference Between
= = ?
Equals() method is used for checking the equality of two objects defined by business logic.
== or the equality operator is used to compare primitives and objects.
14. State Two Differences Between an Inner Class and a Subclass
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.
15. How Are Destructors Defined in Java?
Since Java has its own garbage collection, no destructors are required to be defined. Destruction of objects is automatically carried by the garbage collection mechanism.
16. Define JSON.
17. Name the Most Important Feature of Java
Java is a platform independent language.
18. What Is an Anonymous Class?
The class defined without a name in a single line of code using new keyword is known as an anonymous class.
19. What Is a JVM?
JVM is the Java Virtual Machine, which is a runtime environment for compiled Java class files.
20. Can a Dead Thread Be Started Again?
No, a thread that is in the dead state can't be started again.
21. Are Arrays of Primitive Data Types?
No. In Java, Arrays are objects.
22. What Are Constructors in Java?
In Java, the constructor is a block of code used to initialize an object.
23. What Are the Types of Constructors?
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, as shown below:
// no-argument constructor
// this would be invoked while object
// of that class created.
publicstaticvoidmain (String args)
// this would invoke default constructor.
Sprint zeal1 = new Sprint();
// Default constructor provides the default
// values to the object like 0, null
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.
// Java Program to illustrate calling of
// parameterized constructor.
// data members of the class.
// contructor would initialized data members
// with the values of passed arguments while
// object of that class created.
Sprint(String name, int id)
this.name = name;
this.id = id;
public static void main (String args)
// this would invoke parameterized constructor.
Sprint zeal1 = new Sprint("adam", 1);
System.out.println("Sprint Name :"+ zeal1.name +" and Sprint Id :" + zeal1.id);
24. Explain Garbage Collection in Java.
In Java, when an object is no longer used or referenced, garbage collection is called and the object is destroyed automatically.
25. What's the Difference Between Stack and Queue?
The difference between a stack and a queue is that the stack is based on the Last in First out (LIFO) principle, and a queue is based on FIFO (First In, First Out) principle.
Additional Interview Tips
Having discussed the phases of an interview and common interview questions, here we have listed some quick interview tips to help you ace the interview:
• Be punctual and show up on time
• Be ready to talk about your achievements — both personal and professional
• Keep a check on your language and avoid using fillers
• Go through the job description and find key requirements you can talk about
• Listen carefully and take time to gather your thoughts before answering
• Dress and behave professionally.
• Avoid interrupting the interviewer
• Carry all the essential documents
• Do a brief research on the company and understand their needs
• Thank the interviewer for his/her time and be polite with criticisms, too
We have tried to gather all the essential information required for the interview, but know that Java is a vast topic and several other questions can be asked, too. Prepare for the interview based on the type of industry you are applying for and some of the sample answers provided here vary with the type of industry.
All in all, be honest and positive — it outshines all other qualities!
Opinions expressed by DZone contributors are their own.