Java Interview Questions for 3 Years Experience
These questions focus on explaining day-to-day Java decisions. Give a small example, describe the failure case, and explain how you would verify the fix. Experience labels are practice guides, not fixed rules about what every interviewer asks.
1. A HashMap lookup stopped working after a key changed. What would you inspect?
Check whether a field used by equals() or hashCode() changed after insertion. The entry may be stored according to the old hash. Prefer immutable keys or remove the entry before changing the key and insert it again afterward. Resizing the map is not a fix for a broken key design.
2. How would you compare two classes that have the same business ID?
Decide whether that ID defines equality for this type. Implement equals() and hashCode() together using the chosen fields. Check null, a different type, equal IDs, and unequal IDs. Think about inheritance before choosing an instanceof-based or exact-class equality check.
3. A list changes during iteration. Why is ConcurrentModificationException unreliable as a correctness check?
Fail-fast detection is best effort. Code should follow the collection's modification rules even if no exception appears. Use supported iterator removal, collect changes for later, or choose a collection with suitable concurrent behavior. Do not catch the exception and assume the processing is complete.
4. When would you use ArrayList, HashSet, and HashMap?
Use ArrayList for an indexed sequence, HashSet for unique values without an order guarantee, and HashMap for key-based lookup. Ask whether duplicates, order, nulls, and concurrent access matter. Explain the operation you need before naming an implementation.
5. Why can a null wrapper fail during arithmetic?
A wrapper such as Integer must be unboxed for arithmetic. Unboxing null throws NullPointerException. Validate missing values or choose a deliberate default before the operation. Replacing every missing value with zero can hide a data problem.
6. How would you stop a worker thread?
Define cooperative cancellation. For interruptible waits, interrupt the thread and handle interruption by cleaning up and exiting. If InterruptedException is caught without being rethrown, restore the interrupt status when appropriate. A shared volatile flag may suit some loops, but it does not wake a blocked wait by itself.
7. Does volatile make count++ safe?
No. The update reads a value, adds one, and writes it back. Two threads can still lose an update. Use AtomicInteger for a suitable independent counter or a lock when several fields must change together.
8. Why does wait() belong inside a while loop?
A thread can wake without the condition becoming true, and another thread can change the condition before the waiter reacquires the lock. Test the condition again while holding the same monitor. Notification is a signal to recheck, not permission to skip the condition.
9. How would you investigate a file-reading bug that appears only on another machine?
Check the actual path, working directory, charset, line endings, permissions, and file contents. Use an explicit charset for text when the format defines one. Confirm the error with a small test file before changing the whole reading layer.
10. What problem does try-with-resources solve?
It closes AutoCloseable resources when the block ends, including exceptional exits. Resources close in reverse declaration order. It does not decide the correct transaction outcome or make an external service available.
11. How do you make a JDBC update with user input?
Use PreparedStatement parameters for values, close resources, and choose the transaction boundary. Parameters cannot stand in for SQL identifiers such as table names; those need a controlled allowlist or another explicit design. Test both successful and failing database operations.
12. What does rollback do after an exception?
It attempts to undo uncommitted changes in that transaction. It does not undo an already committed transaction, and external actions such as sending an email are separate. Preserve the original error if rollback itself fails.
13. Why can a cache cause a memory leak?
If a reachable cache keeps entries forever, the collector must keep those reachable objects. Add a suitable size or expiry policy and observe actual retention. Garbage collection does not remove data that the application still references.
14. When would you choose composition over inheritance?
Choose composition when one object uses another object's behavior and may need to replace it. Choose inheritance when the subtype can honor the parent contract. Avoid extending a class only to reuse a few lines if it creates an unsuitable type relationship.
15. How would you make a bug report useful to another developer?
Include the smallest input, expected result, actual result, error details, and environment. Explain what you already ruled out. A reproducible example is more useful than a large screenshot with no execution steps.
Practice an Answer
Choose one question and explain it with a situation you have actually worked on. Separate the symptom, cause, change, and result. If you have not used a feature in production, say so and explain the small test you would build to explore it.
See you in next page....🙂