Home  Java   Tough java ...

tough java interview questions and answers

Here’s a list of tough Java interview questions along with detailed explanations/answers.


1. What is the difference between == and equals() in Java?

Answer:

Example:

String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false (different objects or different memory location)
System.out.println(s1.equals(s2)); // true (same content)

2. Explain the Java memory model. How does garbage collection work?

Answer:


3. What is the difference between HashMap, TreeMap, and LinkedHashMap?

FeatureHashMapTreeMapLinkedHashMap
OrderNo orderSorted by keyInsertion order
Null Key1No1
PerformanceO(1)O(log n)O(1)
ImplementationHash tableRed-Black TreeHash table + linked list

4. Explain volatile keyword. When is it used?

Answer:

volatile boolean flag = true;

5. What is the difference between synchronized and ReentrantLock?

FeaturesynchronizedReentrantLock
SyntaxKeywordClass
FlexibilityLess flexibleMore (tryLock, lockInterruptibly)
FairnessNoCan be fair
UnlockAutomaticMust call unlock() explicitly

6. What is the difference between final, finally, and finalize()?


7. How does Java handle Integer caching?

Integer a = 100; // cached
Integer b = 100;
System.out.println(a == b); // true
Integer c = 200;
Integer d = 200;
System.out.println(c == d); // false

8. Explain Java ClassLoader hierarchy.


9. Difference between HashMap and ConcurrentHashMap

FeatureHashMapConcurrentHashMap
Thread SafeNoYes
Null Key/ValueYesNo null keys/values
LockingNoneSegment-level locking (Java 8 uses CAS and bin-level locking)

10. Explain Deadlock and how to prevent it.

Answer:


11. Difference between ==, equals(), and hashCode()


12. What is the difference between Checked and Unchecked Exceptions?


13. How does Java pass-by-value work?

void modify(List<String> list) {
    list.add("Hi"); // works
    list = new ArrayList<>(); // no effect outside
}

**14. Explain Java 8 Stream API and Collector.collect()

List<String> names = Arrays.asList("John","Jane","Jack");
List<String> filtered = names.stream()
                             .filter(n -> n.startsWith("J"))
                             .collect(Collectors.toList());

15. What are Soft, Weak, and Phantom References in Java?

TypeGC BehaviorUsage
SrongReferenceNot Collected unless nullDefault ref
SoftReferenceCollected only if memory lowCache
WeakReferenceCollected eagerlyWeakHashMap
PhantomReferenceOnly for post-mortem cleanupReferenceQueue

16. How do HashMap collisions get resolved in Java 8+?


**17. Explain Volatile + Double-checked Locking Singleton

public class Singleton {
    private static volatile Singleton instance;
    private Singleton() {}
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized(Singleton.class) {
                if (instance == null) instance = new Singleton();
            }
        }
        return instance;
    }
}

18. Explain CAS (Compare-And-Swap)

CAS → atomic operation to update value without locking. Used in ConcurrentHashMap, AtomicInteger, and other lock-free structures.

19. Explain transient and static in Serialization

20. How does the JVM manage memory?

Memory areas:

21. What happens inside the JVM when you execute a Java program?

Published on: Oct 05, 2025, 10:55 AM  
 

Comments

Add your comment