Do you have what it takes to ace a Java Interview? We are here to help you in consolidating your knowledge and concepts in Java. Before we begin, let’s understand what Java is all about.
The following article will cover all the popular Core Java interview questions, String Handling interview questions, Java 8 interview questions, Java multithreading interview questions, Java OOPs interview questions, Java exception handling interview questions, collections interview questions, and some frequently asked Java coding interview questions.
Go through all the important questions to enhance your chances of performing well in the Java Interviews. The questions will revolve around the basic, core & advanced fundamentals of Java.
🌟 Top 25+ Java Interview Questions and Answers
1. What is Java?
Answer:
Java is a high-level, object-oriented programming language developed by Sun Microsystems (now Oracle) in 1995. It is platform-independent because Java code is converted into bytecode, which runs on the Java Virtual Machine (JVM) on any operating system (Windows, Mac, Linux).
2. Why is Java called platform-independent?
Answer:
Because Java code is compiled into bytecode, not machine-specific code. The bytecode can run on any OS with a JVM installed. This means “Write Once, Run Anywhere”.
3. What is JVM, JRE, and JDK?
Answer:
-
JVM (Java Virtual Machine): Runs the Java bytecode, making Java platform-independent.
-
JRE (Java Runtime Environment): Contains JVM + libraries needed to run Java applications.
-
JDK (Java Development Kit): Contains JRE + development tools like compiler (javac) to develop Java programs.
4. What is the difference between JDK and JRE?
Answer:
| JDK | JRE |
|---|---|
| Used to develop and run programs | Used only to run programs |
| Contains JRE + compiler + tools | Contains JVM + libraries |
5. What are the features of Java?
Answer:
-
Simple: Easy to learn if you know C/C++.
-
Object-Oriented: Everything is an object.
-
Platform Independent
-
Secure: No pointer concepts, has security manager.
-
Robust: Strong memory management and exception handling.
-
Multithreaded: Supports multiple threads execution.
-
Distributed: Supports networking and distributed computing.
-
High Performance: Due to Just-In-Time (JIT) compiler.
6. What is an object?
Answer:
An object is a real-world entity with state and behaviour. For example, a Car is an object having states (color, model) and behaviours (run, brake). In Java, objects are created from classes.
7. What is a class?
Answer:
A class is a blueprint or template to create objects. It defines variables (states) and methods (behaviours) of an object.
class Car {
String color;
void run() {
System.out.println("Car is running");
}
}
8. What is the difference between == and .equals() in Java?
Answer:
-
== operator: Compares references (memory addresses).
Example:obj1 == obj2checks if both point to the same object. -
.equals() method: Compares actual values (content) if it is overridden in the class.
Example:str1.equals(str2)checks if both strings have same characters.
9. What are constructors?
Answer:
A constructor is a special method with the same name as the class, used to initialize objects. It has no return type, not even void.
Example:
class Student {
Student() {
System.out.println("Constructor called");
}
}
10. What is inheritance?
Answer:
Inheritance allows a class (child/subclass) to acquire properties and methods of another class (parent/superclass). This promotes code reusability.
Example:
class Animal {
void eat() { System.out.println("eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("barking..."); }
}
11. What is method overloading?
Answer:
When multiple methods in the same class have the same name but different parameters, it is called method overloading. It increases readability.
Example:
void sum(int a, int b)
void sum(double a, double b)
12. What is method overriding?
Answer:
When a subclass provides a specific implementation of a method already present in its parent class, it is called method overriding. It is used for runtime polymorphism.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
13. What is Polymorphism?
Answer:
Polymorphism means many forms. In Java, it allows an object to take multiple forms:
-
Compile-time polymorphism: Method Overloading
-
Run-time polymorphism: Method Overriding
14. What is Abstraction?
Answer:
Abstraction means hiding implementation details and showing only functionality.
Example: Driving a car without knowing how the engine works.
In Java, it is achieved via:
-
Abstract classes
-
Interfaces
15. What is Encapsulation?
Answer:
Encapsulation is binding data (variables) and code (methods) together into a single unit (class) and keeping them safe from outside interference by making variables private and accessing them via public getters/setters.
16. What is an interface in Java?
Answer:
An interface is a collection of abstract methods (from Java 8, it can also have default and static methods). It is used to achieve 100% abstraction and multiple inheritance in Java.
Example:
interface Animal {
void eat();
}
17. Difference between abstract class and interface?
| Abstract Class | Interface |
|---|---|
| Can have abstract and non-abstract methods | Only abstract methods (till Java 7) |
| Can have constructors | Cannot have constructors |
| Supports single inheritance | Supports multiple inheritance |
18. What is a package in Java?
Answer:
A package is a group of related classes and interfaces. It helps to avoid name conflicts and makes code manageable.
Example: java.util package has utility classes like Scanner, Arrays, etc.
19. What is exception handling?
Answer:
Exception handling is a mechanism to handle runtime errors so normal flow of program is maintained. Keywords used are:
-
try: Code that might throw exception.
-
catch: Code to handle exception.
-
finally: Code that executes regardless of exception.
20. What is the difference between checked and unchecked exceptions?
Answer:
| Checked Exception | Unchecked Exception |
|---|---|
| Checked at compile-time | Checked at runtime |
| Example: IOException | Example: ArithmeticException |
21. What is a thread in Java?
Answer:
A thread is a lightweight sub-process. Java supports multithreading, meaning it can execute multiple threads simultaneously for better performance.
22. How to create a thread in Java?
Answer:
Two ways:
-
Extending Thread class
-
Implementing Runnable interface
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
23. What is synchronization?
Answer:
Synchronization is the process to control access of multiple threads to shared resources to prevent data inconsistency.
24. What is the difference between ArrayList and LinkedList?
| ArrayList | LinkedList |
|---|---|
| Uses dynamic array | Uses doubly linked list |
| Slow for insertion/deletion | Faster for insertion/deletion |
| Fast for random access | Slow for random access |
25. What is the difference between String, StringBuilder, and StringBuffer?
| Â | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-safe | Yes | No | Yes |
| Performance | Slow for concat | Fast | Slightly slower than StringBuilder |
26. What is garbage collection in Java?
Answer:
Garbage collection is a process by which JVM automatically removes unused objects from memory, to free space and improve performance.
27. What are access modifiers?
Answer:
They define the scope of variables, methods, and classes. Types are:
-
private: Within class
-
default: Within package
-
protected: Within package and subclasses
-
public: Everywhere
Here are 5 additional advanced-level Java interview questions with detailed and easy explanations to strengthen your preparation beyond fundamentals:
28. What is the difference between HashMap and Hashtable in Java?
Answer:
Both HashMap and Hashtable implement the Map interface and store data as key-value pairs, but there are key differences:
| HashMap | Hashtable |
|---|---|
| Not synchronized – not thread-safe, better performance in single-threaded applications | Synchronized – thread-safe, slower compared to HashMap |
| Allows one null key and multiple null values | Does not allow null keys or null values |
| Introduced in Java 1.2 | Introduced in Java 1.0 |
Can be made synchronized using Collections.synchronizedMap() |
By default, all methods are synchronized |
| Generally preferred in non-threaded applications for better performance | Used in multithreaded legacy applications |
âś… Example:
Map<String, Integer> map = new HashMap<>();
map.put(null, 1); // works fine
Map<String, Integer> table = new Hashtable<>();
table.put(null, 1); // throws NullPointerException
29. Explain the concept of String Pool in Java.
Answer:
In Java, String is immutable, and to optimize memory usage, JVM maintains a String Pool (String Intern Pool) in the heap memory.
âś… How it works:
-
When a String literal is created:
String s1 = "Hello";
String s2 = "Hello";
Both s1 and s2 point to the same object in the String Pool, avoiding duplicate objects.
âś… Using new keyword:
String s3 = new String("Hello");
This creates a new object in the heap, not in the String Pool, even if “Hello” already exists in the pool.
âś… Benefits:
-
Saves memory by reusing string literals.
-
Improves performance in applications with many identical strings.
âś… Interning:
If you want to add a String created with new into the String Pool, you can use:
s3 = s3.intern(); // adds to pool if not present, returns reference from pool
30. Explain the lifecycle of a thread in Java.
Answer:
A Java thread has five main states as defined by the Thread class:
-
New:
When a thread instance is created usingnew Thread(), butstart()has not been called yet. -
Runnable:
After callingstart(), the thread is ready to run and is waiting for CPU allocation. -
Running:
When the thread scheduler picks it, the thread starts executing itsrun()method. -
Blocked / Waiting:
-
Blocked: Thread waiting to acquire a lock (synchronized block).
-
Waiting: Thread waiting indefinitely for another thread to perform a particular action (e.g.,
wait()).
-
-
Timed Waiting:
Thread waiting for another thread to perform an action within a specified time (e.g.,sleep(1000)orwait(1000)). -
Terminated (Dead):
Once therun()method finishes, the thread is terminated and cannot be restarted.
âś… Thread state transitions are important for multithreading-based interview questions, especially for system design and concurrency roles.
31. What is the difference between abstract class and interface in Java 8 onwards?
Answer:
Before Java 8, interfaces could only have abstract methods, while abstract classes could have concrete (implemented) methods. From Java 8 onwards, interfaces were enhanced:
| Abstract Class | Interface (Java 8 onwards) |
|---|---|
| Can have constructors | Cannot have constructors |
| Supports instance variables | No instance variables; only public static final constants |
| Can have abstract and concrete methods | Can have abstract, default, and static methods |
| Supports single inheritance | Supports multiple inheritance of type (because a class can implement multiple interfaces) |
| Used for common base with shared code | Used for defining contract or capability (e.g., Runnable, Serializable) |
âś… Default methods: Introduced in Java 8 to allow interfaces to provide default implementations.
Example:
interface MyInterface {
default void show() {
System.out.println("Default method in interface");
}
}
âś… Static methods: Also allowed from Java 8 onwards.
32. Explain the Java Memory Model (JMM) and how it relates to multithreading.
Answer:
The Java Memory Model (JMM) defines how threads interact through memory and what behaviors are allowed in multithreaded programs. It specifies:
-
Main concepts:
-
Shared variables: Variables accessible by multiple threads.
-
Thread working memory: Each thread can cache variables locally.
-
-
Visibility Problem:
Changes made by one thread may not be visible immediately to other threads without proper synchronization due to caching. -
Happens-before relationship:
Defines rules for memory visibility:-
If action A happens-before action B, then changes made by A are visible to B.
-
E.g., unlocking a lock happens-before any subsequent locking of the same lock.
-
-
Volatile keyword:
Declaring a variable asvolatileensures:-
Visibility: Changes are visible to all threads immediately.
-
No atomicity: Does not guarantee atomic operations like increment.
-
-
Synchronization:
-
Ensures visibility and atomicity.
-
Synchronized blocks/methods create memory barriers where:
-
All changes before entering synchronized block are visible to thread.
-
Changes made inside synchronized block are flushed to main memory when exiting.
-
-
âś… Why JMM is important in interviews:
Understanding JMM is crucial for:
-
Designing thread-safe applications
-
Avoiding race conditions, deadlocks, and visibility issues
-
Writing correct concurrent code
Conclusion
In this blog, we explored advanced C# interview questions covering essential concepts like Generics, Reflection, Threading, File Handling, Performance Optimization, Design Patterns, and Real-world Concurrency. Whether you’re preparing for your first job or leveling up to senior roles, mastering these topics will give you a significant edge in technical interviews.
If you found this guide helpful, follow Logic Lense for more career-boosting tech content, coding tips, and interview prep resources.
💬 Got questions or thoughts? Leave a comment below — we’d love to hear from you!
Subscribe to ASP.NET Core Newsletter.
Want to advance your career in .NET and Architecture? Join 1,000+ readers of my newsletter. Each week you will get 1 practical tip with best practices and real-world examples.

