Fundamentals 39 min read

Top 10 Tricky Java Interview Questions and Answers

This article compiles ten of the most challenging Java interview questions, covering topics such as wait/notify placement, lack of multiple inheritance, operator overloading, string immutability, password storage, double‑checked locking singleton, deadlock creation and resolution, serialization pitfalls, and static method overriding, with detailed explanations and code examples.

Java Captain
Java Captain
Java Captain
Top 10 Tricky Java Interview Questions and Answers

This article gathers ten of the most difficult Java interview questions, providing thorough explanations and code samples to help developers prepare for core‑Java interviews.

1. Why are wait , notify and notifyAll declared in Object rather than Thread ? They belong to the object monitor model; every object can act as a monitor, allowing any thread to synchronize on any object and use these methods for inter‑thread communication.

2. Why does Java not support multiple inheritance? The diamond problem creates ambiguity in method resolution, and Java prefers a simpler, more maintainable design. Interfaces provide multiple inheritance of type without the implementation conflicts.

3. Why does Java not support operator overloading? Simplicity, reduced compiler complexity, fewer programming errors, and better JVM optimization are the main reasons.

4. Why is String immutable and final ? Immutability enables safe sharing, string‑pool caching, stable hash codes for use as HashMap keys, and prevents security issues when strings represent file names, URLs, etc.

5. Why store passwords in a char[] instead of a String ? A char[] can be cleared after use, avoiding the risk of the password lingering in memory or the string pool.

6. How to create a thread‑safe singleton with double‑checked locking?

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

Enum singletons are even simpler: public enum EasySingleton { INSTANCE; } 7. How to create and fix a deadlock in Java? The example shows two methods acquiring locks on String.class and Integer.class in opposite order, causing a deadlock. The fix is to acquire the locks in a consistent order.

public class DeadLockFixed {
    public void method1() {
        synchronized (Integer.class) {
            synchronized (String.class) { /* ... */ }
        }
    }
    public void method2() {
        synchronized (Integer.class) {
            synchronized (String.class) { /* ... */ }
        }
    }
}

8. What happens if a Serializable class contains a non‑serializable member? Serialization fails with NotSerializableException; the field should be marked transient or handled manually.

9. Why must wait be called inside a synchronized block? The thread must own the object's monitor; otherwise an IllegalMonitorStateException is thrown, and race conditions can occur.

10. Can static methods be overridden in Java? No; they are hidden, not overridden. The call is resolved at compile time based on the reference type.

public class CanWeOverrideStaticMethod {
    public static void main(String[] args) {
        Screen scrn = new ColorScreen();
        scrn.show(); // warning: static method should be called on class name
    }
}
class Screen {
    public static void show() { System.out.println("Static method from parent class"); }
}
class ColorScreen extends Screen {
    public static void show() { System.err.println("Overridden static method in Child Class"); }
}

These questions and answers cover core Java concepts such as concurrency, object design, serialization, and language limitations, providing valuable insight for both junior and senior developers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

concurrencyinterviewOOPDesign
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.