2025 62 curated interview questions

62 Java Interview Questions

Master your next Java interview with our comprehensive collection of questions and expert-crafted answers. Get prepared with real scenarios that top companies ask.

Master Java interviews with expert guidance

Prepare for your Java interview with proven strategies, practice questions, and personalized feedback from industry experts who've been in your shoes.

  • Thousands of mentors available
  • Flexible program structures
  • Free trial
  • Personal chats
  • 1-on-1 calls
  • 97% satisfaction rate

Study Mode

1. Could you distinguish between equals() method and == operator?

The equals() method and the == operator in Java are both used to compare two objects, but they work in different ways.

The == operator compares the references of objects, not their content. It checks to see if the two references point to exactly the same object in memory. This means that if you have two separate but identical objects, == will return false.

On the other hand, the equals() method is used for comparing the content or state of two objects. It checks the values within an object. For example, when comparing two strings, equals() will take into account the characters in the string, while == will just check if they're the same physical object.

By default, the equals() method behaves the same as the == operator. But it's often overridden in classes to enhance its behavior based on attributes of an object. For instance, the String class overrides the equals() method to compare the actual characters within the strings.

2. Can you illustrate how to read and write files in Java?

To read and write files in Java, you typically use the java.io or java.nio package. Let me provide a brief example using java.io:

To write to a file, you can use a FileWriter and BufferedWriter:

try (BufferedWriter bw = new BufferedWriter(new FileWriter("filename.txt"))) { bw.write("This is some file content"); } catch (IOException e) { e.printStackTrace(); }

This will create a file named "filename.txt" and write the string "This is some file content" to it. The try-with-resources statement ensures that each resource is closed at the end of the statement.

Similarly, to read from a file, you can use a FileReader and BufferedReader:

try (BufferedReader br = new BufferedReader(new FileReader("filename.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }

This will read the file "filename.txt" and print each line in the file to the console. The BufferedReader's readLine() method returns a line of text with each call.

Also, since Java 7, Files class from java.nio package provides the method Files.readAllLines(Path), which makes it easy to read all lines of a file into a List of Strings. Similarly, Files.write(Path, Iterable) allows to write a list of strings into a file.

3. What is Polymorphism in Java?

Polymorphism in Java is an object-oriented programming concept that refers to the ability of a method, object, or variable to take on multiple forms. The term polymorphism comes from Greek and means having multiple forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism.

Compile-time polymorphism is achieved through method overloading, where methods of the same class share the same name but have different parameters.

Runtime polymorphism is achieved through method overriding, which occurs when a subclass provides a specific implementation of a method that's already provided by its parent class. In this case, an object instance is decided at runtime which method implementation to call, of base class or derived class.

Polymorphism promotes flexibility and reusability in code by allowing you to use one interface with different underlying forms. For example, if you have a parent class “Animal”, and subclasses “Dog”, “Cat”, and “Bird”, and they each have their own implementation of a sound() method, you could call the sound method on an animal without knowing what type of animal it is, and it would produce the correct sound depending on the type of Animal it is. This is polymorphism in action.

No strings attached, free trial, fully vetted.

Try your first call for free with every mentor you're meeting. Cancel anytime, no questions asked.

4. Can you define Method Overloading and Method Overriding?

Method Overloading and Method Overriding are two concepts in Java that allow a class to have two or more methods with the same name but different purposes.

Method Overloading is a feature that allows a class to have more than one method having the same name, but different in parameters. The parameter list could differ in their data type, sequence of data types when multiple parameters are passed, or even their numbers. Overloading is typically used when two methods perform a similar function but on different input types.

On the other hand, Method Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by its super/parent class. It is used for runtime polymorphism and for implementing different behavior in a subclass.

In method overriding, the method in the child class should have the same name, return type, and parameters as the one in its parent class. The keyword for achieving method overriding is 'extends'.

So, method overloading means the same method name but different parameter list, whereas method overriding means the same method name, same parameter list but it is in the subclass.

5. Can you explain the difference between JDK, JRE, and JVM?

Sure, let's break it down.

JVM (Java Virtual Machine) can be described as the basis of the Java platform. It's a runtime environment for executing bytecode. It doesn't understand Java source code, rather, it runs compiled Java code, which is typically in the form of .class files. JVM performs crucial tasks like loading code, verifying code, executing code, and providing runtime environment.

JRE (Java Runtime Environment), on the other hand, is essentially a software package that contains what is required to run a Java program. Essentially, it's the implementation of JVM which physically exists. It contains set of libraries and other files that JVM uses at runtime.

JDK (Java Development Kit) is a development tool necessary to compile, document and package Java programs. The JDK is a complete package for Java developers, containing JRE along with various development tools like the Java source compilers, bundling and deployment tools, debuggers, development libraries and more.

So, in essence, JDK is for developers who need to compile Java code, JRE is for users running Java applications, and JVM is an abstract machine to run the bytecode, which is contained in the JRE.

6. Define Inheritance in Java.

In Java, inheritance is a concept of Object-Oriented Programming which allows a class to inherit properties and behavior from another class. The class from which properties are inherited is called the superclass or parent class, and the class which inherits those properties is called the subclass or child class.

Inheritance is a way to establish a relationship between classes in terms of dominance and hierarchy. It helps to reuse, extend or modify the attributes and behaviors which are already defined in other classes.

In Java, the 'extends' keyword is used to indicate that a class is to inherit from a superclass. The subclass can access all non-private attributes and methods of the superclass and can also add new fields and methods.

Inheritance helps to organize and structure software programs. It provides a mechanism that allows a certain class to adopt the features of another, which leads to code reusability and can improve code organization and readability. It's also the mechanism that allows for polymorphism, where a subclass can be treated like its parent class.

7. What are the primary features of the Java programming language?

Sure, here we go.

Java is a high-level language known for its "Write Once, Run Anywhere" capability. Here are some of its key features:

Firstly, Java is object-oriented, meaning it revolves around the concept of objects which contain data and methods that manipulate this data.

Secondly, it's platform-independent: one of Java's biggest advantages is the ability to move easily from one computer system to another. You write the code once and you can run it anywhere that has a JVM, whether it's Windows, Mac, Linux, etc.

Thirdly, Java is strongly typed. You have to define the type of every variable you declare, which reduces errors and improves the clarity of code.

Additionally, Java is multi-threaded, enabling you to write programs that do multiple things simultaneously. Each thread in a multi-threaded process performs a different job.

Java has automatic memory management or garbage collection, so you don't have to manually manage the memory allocation and deallocation.

Lastly, it has a rich set of APIs. Java has a wide variety of libraries that provide reusable functions to handle common software development tasks.

So in summary, Java is object-oriented, platform-independent, strongly typed, multi-threaded, automatic memory managed, and well-supported with rich APIs.

8. Explain the difference between abstract class and interface.

Abstract classes and interfaces in Java are ways to achieve abstraction, but they are used in different contexts and have different rules.

An abstract class in Java is a class that can't be instantiated and is typically used as a base class for other classes. It can contain both abstract (methods without a body) and non-abstract methods (methods with a body). Abstract classes are a way to create a degree of implementation inheritance because sub-classes can inherit the implemented methods.

An interface, on the other hand, is a completely abstract entity that can only declare methods and final fields. All the methods declared inside an interface are abstract by default, and all fields are public, static, and final. An interface is typically used to encapsulate a functionality that a class should implement. Since Java 8, interfaces have been able to contain default and static methods, but they still can't have instance fields or a constructor.

One fundamental difference between abstract classes and interfaces lies in the fact that a class can implement multiple interfaces but can only extend one abstract class. So, if you want to specify a set of methods that a class should implement, you tend to use interfaces, whereas for a partial implementation that other classes may want to share, you'd often use an abstract class.

Master Your Java Interview

Essential strategies from industry experts to help you succeed

Research the Company

Understand their values, recent projects, and how your skills align with their needs.

Practice Out Loud

Don't just read answers - practice speaking them to build confidence and fluency.

Prepare STAR Examples

Use Situation, Task, Action, Result format for behavioral questions.

Ask Thoughtful Questions

Prepare insightful questions that show your genuine interest in the role.

9. Can you describe the concept of OOP (Object Oriented Programming) in Java?

10. What is encapsulation in Java?

11. What are the core components of a Java program?

12. Can you differentiate between JDK, JRE, and JVM?

13. What are the primary data types in Java?

14. What is a constructor in Java?

15. How does exception handling work in Java?

16. Could you clarify the difference between checked and unchecked exceptions in Java?

17. What are Java packages and how are they used?

18. How do you implement multi-threading in Java?

19. What is garbage collection in Java and why is it important?

20. Explain the concept of Java servlets.

21. Can you explain the JDBC API?

22. What is a JavaBean?

23. What are Java applets?

24. Describe how hashcode() and equals() methods are used in Java.

25. What is Java Swing?

Get Interview Coaching from Java Experts

Knowing the questions is just the start. Work with experienced professionals who can help you perfect your answers, improve your presentation, and boost your confidence.

Mottakin Chowdhury

Mottakin Chowdhu…

Senior Software Engineer @ Booking.com

(12)

I am a Senior Software Engineer at Booking.com, the largest travel company in the world. Before joining here, I was working as a Senior Software …

Software Engineering Backend Microservices
View Profile
Kuai Yu

Kuai Yu

Director of Engineering @ Unicorn, ex-Two Sigma, Citadel, Google

(15)

👋 Hi! I’m Kuai, currently a Director of Engineering at a Fintech unicorn. With experience at Two Sigma, Google, and Citadel, I have a decade-long …

Career Growth Engineering Leadership Career Change
View Profile
Benjamin Kaiser

Benjamin Kaiser

Principal Software Engineer @ Microsoft

(28)

Hello, I'm Ben! I've spent the majority of my career as a Software Engineer at Microsoft. I'm passionate about open source, crypto and the future …

JavaScript React NodeJS
View Profile
Dan Page

Dan Page

Lead Full Stack Engineer

(32)

I'm a self-taught Developer with experience at large B2C companies like Deliveroo and Memrise. I have a track record of helping people get their first …

JavaScript React Typescript
View Profile
Catalin Rosu

Catalin Rosu

Tech Lead / Development Manager

(22)

Tech Lead / Senior software engineer with 20+ years in full cycle development, dedicated to mentoring growth. Throughout my career, I have honed my skills …

C# .NET SQL
View Profile
Manish Gharat

Manish Gharat

Senior Software Engineer - Frontend @ Miro

(19)

Are you a junior developer eager to accelerate your career in web development? Do you seek expert guidance on learning the most relevant and up-to-date …

JavaScript Typescript React
View Profile

Still not convinced? Don't just take our word for it

We've already delivered 1-on-1 mentorship to thousands of students, professionals, managers and executives. Even better, they've left an average rating of 4.9 out of 5 for our mentors.

Get Interview Coaching
  • "Naz is an amazing person and a wonderful mentor. She is supportive and knowledgeable with extensive practical experience. Having been a manager at Netflix, she also knows a ton about working with teams at scale. Highly recommended."

  • "Brandon has been supporting me with a software engineering job hunt and has provided amazing value with his industry knowledge, tips unique to my situation and support as I prepared for my interviews and applications."

  • "Sandrina helped me improve as an engineer. Looking back, I took a huge step, beyond my expectations."

Complete your Java interview preparation

Comprehensive support to help you succeed at every stage of your interview journey