What are the four pillars of OOP in Java?
Encapsulation, Inheritance, Polymorphism, and Abstraction.
Explain encapsulation with an example.
Hiding internal state and exposing behavior via methods; e.g., private fields with public getters/setters in a class.
What is the difference between abstraction and encapsulation?
Abstraction focuses on exposing essential behavior/contract; encapsulation focuses on hiding implementation details and controlling access.
What is polymorphism? Give two forms.
The ability of a single interface to represent different underlying forms. Compile-time (method overloading) and runtime (method overriding).
What is the difference between overloading and overriding?
Overloading: same method name, different parameters within a class. Overriding: subclass provides its own implementation of a superclass method with same signature.
What is an interface vs an abstract class?
Interface: contract of methods (and default/static methods); abstract class: can hold state and partial implementation. Java 8+ interfaces can have default/static methods.
What are the main access modifiers in Java?
private, default (package-private), protected, public.
What is the ‘final’ keyword used for (variable, method, class)?
Variable: constant reference; Method: cannot be overridden; Class: cannot be subclassed.
What is the difference between == and equals()?
== compares references for objects; equals() compares logical equality. For primitives, == compares values.
What is an enum in Java and a common pattern using it?
A special class for fixed constants. Pattern: enum-based singleton (thread-safe).
What is the difference between checked and unchecked exceptions?
Checked must be declared/handled; unchecked (RuntimeException) need not be declared and often indicate programming errors.
Explain try-with-resources.
A try statement that declares AutoCloseable resources and closes them automatically.
How do you create an immutable class in Java?
Private final fields, no setters, defensive copies for mutables, final class, init via constructor only.
List the main Java Collections interfaces.
Collection, List, Set, Queue, Deque, Map (separate hierarchy).
ArrayList vs LinkedList trade-offs?
ArrayList: O(1) random access, amortized O(1) append; LinkedList: O(1) add/remove at ends but O(n) random access.
HashSet vs TreeSet vs LinkedHashSet?
HashSet: unordered O(1) avg; TreeSet: sorted O(log n); LinkedHashSet: insertion order O(1) avg.
How do HashMap and ConcurrentHashMap differ?
HashMap is not thread-safe; ConcurrentHashMap supports concurrent access with bucket-level locking and non-blocking reads.
What underlies ArrayDeque and common uses?
Resizable circular array; efficient stack/queue ops at both ends.
Binary search vs linear search complexity?
Summary:
Linear search is slower for large datasets (O(n)), works on unsorted data.
Binary search is much faster (O(log n)), but only works on sorted data.
Linear search checks each element one by one until it finds the target or reaches the end.
Time complexity: O(n) (where n is the number of elements)
Binary search repeatedly divides a sorted array in half to find the target.
Time complexity: O(log n) (requires the array to be sorted)
Difference between process and thread?
Process has its own memory; threads share memory within a process.
What are synchronized blocks/methods for?
Synchronized blocks and methods in Java are used to control access to shared resources in multithreaded environments.
They prevent multiple threads from executing critical sections of code simultaneously, avoiding data inconsistency and race conditions.
By locking an object or method, only one thread can execute the synchronized code at a time.
What is a deadlock and how to prevent it?
A deadlock is a situation in concurrent programming where two or more threads are blocked forever, each waiting for the other to release a resource.
Prevention techniques:
Acquire locks in a fixed global order.
Use lock timeouts.
Minimize the scope and number of locks.
Avoid nested locking.
Use higher-level concurrency utilities (e.g., java.util.concurrent).
Deadlocks can be avoided by careful design and by ensuring threads do not hold resources while waiting for others.
Explain ThreadPoolExecutor benefits.
Reuses threads, controls concurrency, manages queuing, provides lifecycle hooks.
What are CompletableFuture and common methods?
Async API: supplyAsync, thenApply, thenCompose, thenAccept, allOf, anyOf, exceptionally, handle, join.