20 Java Interview Questions That Hiring Managers Actually Ask

Forget memorizing trivia. We're breaking down the 20 core Java interview questions that reveal your true problem-solving skills and depth of knowledge.
Offer Ends Jan 10th : Get 100 Free Credits on Signup Claim Now

Forget memorizing trivia. We're breaking down the 20 core Java interview questions that reveal your true problem-solving skills and depth of knowledge.
I once interviewed a developer who could write flawless, complex algorithms on the whiteboard in minutes. I was impressed. Then I asked him a simple question: "Why did you choose an ArrayList here instead of a LinkedList?" He froze. He couldn't articulate the trade-offs. He had the 'what' down cold, but the 'why' was a complete mystery. He didn't get the job.
That's the secret nobody tells you about Java interviews. They aren't just about regurgitating facts. They're about demonstrating a deep, practical understanding of the tools you use every day. An interviewer wants to see your thought process. They want to know you can make sound architectural decisions, not just follow a tutorial.
This isn't just another list of questions. We're going to dissect what the interviewer is really asking, how to structure your answer, and the common traps to avoid. Let's get you ready for the real thing.
These questions test your fundamental grasp of the Java language and platform. Get these wrong, and it's an immediate red flag.
1. Explain the difference between final, finally, and finalize.
final: A keyword used to apply restrictions. It can be used with variables (to create constants), methods (to prevent overriding), and classes (to prevent inheritance).finally: A block used in a try-catch statement. The finally block always executes, whether an exception is thrown or not. It's crucial for cleanup code, like closing database connections or file streams.finalize(): A method from the Object class. The garbage collector calls this method on an object just before reclaiming its memory. Stress that it's not guaranteed to run and should generally be avoided for resource cleanup in favor of try-with-resources or finally.Pro Tip: Mentioning
try-with-resourcesas the modern, preferred alternative tofinallyfor resource management shows you're up-to-date with Java 7+ features.
2. Why are String objects immutable in Java?
String with a database username to a method, you know that method can't change it."hello" exists in the JVM.String objects inherently thread-safe. You can share them across multiple threads without worrying about synchronization issues.3. Explain the contract between equals() and hashCode().
HashMap and HashSet work under the hood? This is a critical concept.equals() method, then they must have the same hashCode().hashCode(), they are not necessarily equal. This is called a hash collision.hashCode() to find the 'bucket' where an object should be stored. Then, they use equals() to find the exact object within that bucket. If you violate the contract, you won't be able to retrieve objects you've stored.4. What is the JVM, JRE, and JDK?
javac), a debugger, and other development tools.write once, run anywhere).5. Explain Garbage Collection in brief.
This is where they test your ability to choose the right tool for the job. Performance often hinges on these decisions.
6. What's the difference between ArrayList and LinkedList?
| Feature | ArrayList | LinkedList |
|---|---|---|
| Underlying Structure | Dynamic Array | Doubly-Linked List (Nodes) |
get(index) Operation | O(1) - Fast | O(n) - Slow (must traverse) |
add(element) Operation | O(1) amortized (slow if resize needed) | O(1) - Fast |
add/remove in middle | O(n) - Slow (requires shifting elements) | O(1) - Fast (if you have the iterator) |
| Memory Overhead | Lower | Higher (due to node wrappers) |
Key Takeaway: Choose
ArrayListwhen you need fast, random access. ChooseLinkedListwhen you'll be doing a lot of insertions and deletions in the middle of the list.
7. How does a HashMap work internally?
equals()/hashCode() contract. Do you really get it?HashMap stores key-value pairs in an array of 'buckets' or 'nodes'.put(key, value), it first calculates the hashCode() of the key.equals() method is used to differentiate between keys in the same bucket.8. What's the difference between Comparable and Comparator?
Comparable: Provides a single, natural ordering for a class. The class itself must implement the Comparable interface and its compareTo() method. For example, a Person class might be naturally ordered by age.Comparator: Provides multiple, external orderings. You can create separate classes that implement the Comparator interface to define different ways to sort objects. For example, you could have one Comparator to sort Person objects by name and another to sort them by height.9. Explain the Java Collections Framework hierarchy.
Collection interface.List (ordered, allows duplicates - ArrayList, LinkedList), Set (unordered, no duplicates - HashSet, TreeSet), and Queue (ordered for processing - PriorityQueue).Map interface is separate from the Collection hierarchy but is considered part of the framework. It deals with key-value pairs (HashMap, TreeMap).This is where senior developers separate themselves from juniors. These topics are complex but essential for building scalable applications.
10. What is the volatile keyword used for?
volatile keyword ensures that changes to a variable are always visible to other threads.volatile, one thread might update its copy, but another thread might not see that change.volatile guarantees that any read of the variable will get the most recent write from main memory.count++, you still need synchronized or AtomicInteger.11. What is the difference between synchronized and a ReentrantLock?
synchronized: A built-in language feature (keyword). It's simpler to use. The lock is automatically acquired and released, even if exceptions occur.ReentrantLock: A library-based class (java.util.concurrent.locks). It offers more flexibility:
tryLock).ReentrantLock is that you must manually release the lock in a finally block to avoid deadlocks.12. Explain the lifecycle of a thread.
new Thread()).running and ready to run states.synchronized block).Object.wait()).Thread.sleep()).13. What is the Executor Framework?
java.util.concurrent) abstracts away thread creation and management.new Thread(runnable).start(), you submit tasks (Runnable or Callable) to a thread pool.ExecutorService and factory methods in Executors (e.g., newFixedThreadPool).No Java developer works in a vacuum. These questions test your knowledge of the tools that build modern applications.
14. What is Dependency Injection (DI) and Inversion of Control (IoC)?
new DatabaseService()), the framework injects these dependencies into your object at runtime.15. What's the difference between @Autowired and @Inject?
@Autowired is Spring's own annotation.@Inject is part of the Java Dependency Injection standard (JSR-330). It's more portable.@Autowired has a required attribute, which @Inject does not. Using @Inject is often seen as a best practice for framework-agnostic code.16. What are Spring Boot Starters?
spring-boot-starter-web dependency automatically pulls in everything you need for a web application: Tomcat, Spring MVC, Jackson for JSON, etc.17. What is Aspect-Oriented Programming (AOP)?
For mid-to-senior roles, you'll get questions that test your ability to think at a higher level.
18. How would you design a URL shortening service like TinyURL?
POST /shorten endpoint that takes a long URL and returns a short URL. A GET /{shortCode} endpoint that redirects to the long URL.short_code (primary key), long_url, created_at, etc.shortCode? You could use a hash function (like MD5 or SHA-1) on the long URL and take the first 6-8 characters. Discuss the possibility of collisions and how to handle them (e.g., retry with a slightly modified input).19. What is the difference between REST and SOAP?
GET, POST, PUT, DELETE). REST is the dominant style for modern web APIs.20. Explain the CAP Theorem.
Remember, the goal isn't just to answer the question. It's to have a conversation that demonstrates your expertise and passion for building great software. Prepare your answers, but more importantly, understand the principles behind them. Good luck.
Stop saying you're a perfectionist. This guide breaks down how to answer the dreaded weakness question with a genuine, strategic response that impresses hiring managers.
Stop reciting your resume. Learn the 'Present-Past-Future' framework to deliver a compelling 90-second pitch that lands you the job in today's market.
Learn how to structure your behavioral interview answers using Situation, Task, Action, Result framework.
Read our blog for the latest insights and tips
Try our AI-powered tools for job hunt
Share your feedback to help us improve
Check back often for new articles and updates
The AI suggestions helped me structure my answers perfectly. I felt confident throughout the entire interview process!