HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
75 Topics
Chapter•390 Minutes

Java Progressive Master Question Bank

75 progressive Java interview questions complete with technical explanations, key takeaways, diagrams, code examples, and interactive knowledge check quizzes.

Q71: What are Shallow Copy versus Deep Copy, and why is Cloneable criticized?

  • Shallow Copy: Copies object field values directly. Primitive values are copied, but object references point to the same shared heap objects.
  • Deep Copy: Recursively duplicates the target object AND all nested child objects, allocating completely independent heap structures.
  • Cloneable Flaws: Marker interface lacking clone method; Object.clone() uses protected native bitwise copying, bypassing constructors. Prefer Copy Constructors or Static Factory Methods.
Key Takeaways
  • Shallow copy shares nested references; Deep copy recursively duplicates object graphs.
  • Prefer Copy Constructors over Cloneable.
Knowledge Check

Why are Copy Constructors preferred over implementing java.lang.Cloneable?

Q72: How does Serialization work, and what is the role of transient and serialVersionUID?

Serialization transforms object state into a byte stream (ObjectOutputStream).

  • serialVersionUID: Unique version identifier verifying that sender and receiver loaded compatible class definitions during deserialization (InvalidClassException on mismatch).
  • transient: Keyword applied to fields that MUST be skipped during serialization (e.g., passwords, transient caches).
Key Takeaways
  • serialVersionUID verifies class compatibility during deserialization.
  • transient fields are excluded from serialized byte streams.
Knowledge Check

What value does a transient int variable hold upon being deserialized from a byte stream?

Q73: How does the Builder Pattern solve telescoping constructor anti-patterns?

When a class contains numerous optional parameters, traditional constructors lead to confusing 'telescoping' parameter list overloads. The Builder Pattern provides a fluent inner builder class step-by-step parameter setting, enforcing parameter validation and producing clean immutable objects.

Builder Pattern Implementation
public class HttpRequest {
    private final String url;
    private final String method;
    private final int timeout;

    private HttpRequest(Builder b) {
        this.url = b.url;
        this.method = b.method;
        this.timeout = b.timeout;
    }

    public static class Builder {
        private String url;
        private String method = "GET"; // Default
        private int timeout = 5000;

        public Builder(String url) { this.url = url; }
        public Builder method(String m) { this.method = m; return this; }
        public Builder timeout(int t) { this.timeout = t; return this; }
        public HttpRequest build() { return new HttpRequest(this); }
    }
}
💡 Fluent builder sets optional parameters and creates immutable HttpRequest objects.
Key Takeaways
  • Eliminates telescoping constructor overloads.
  • Constructs validated immutable objects using fluent method chaining.
Knowledge Check

What primary problem does the Builder Pattern address during object creation?

Q74: How do you implement a Thread-Safe Singleton using Double-Checked Locking or Bill Pugh Helper?

  • Double-Checked Locking: Checks instance nullity twice with a synchronized block and a volatile reference variable (to prevent instruction reordering).
  • Bill Pugh Singleton: Uses a static inner helper class. The instance is allocated strictly when the inner class is loaded on demand by the ClassLoader (Thread-safe, lazy, zero lock overhead).
  • Enum Singleton: Best practice per Joshua Bloch (handles serialization and reflection attacks automatically).
Bill Pugh Singleton Pattern
public class Singleton {
    private Singleton() {} // Private constructor

    private static class Helper {
        private static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return Helper.INSTANCE; // Lazy thread-safe load
    }
}
💡 Classloader thread safety guarantees single instance allocation on Helper class access.
Key Takeaways
  • Double-Checked Locking MUST declare the instance volatile.
  • Bill Pugh Static Helper provides thread-safe lazy loading without locks.
  • Enum Singletons prevent reflection and serialization duplication.
Knowledge Check

Why MUST the instance reference be declared volatile in a Double-Checked Locking Singleton implementation?

Q75: What are the differences between Factory Method and Abstract Factory Design Patterns?

  • Factory Method: Defines a single method interface for instantiating an object, letting subclasses decide which concrete class to create (ShapeFactory.getShape("CIRCLE")).
  • Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes (GUIFactory creating WindowsButton + WindowsCheckbox).
Key Takeaways
  • Factory Method = Creates a single product item. Abstract Factory = Creates families of related products.
Knowledge Check

Which design pattern provides an interface for producing families of related or dependent objects?