These java interview questions and answers run from freshers to experienced developers — covering OOP, the collections framework, exceptions, multithreading, JVM memory and the string pool — each with a concise, correct model answer and short code where it clarifies the concept.
Beginner java interview questions
Q: What are the four pillars of OOP in Java?
A: They are encapsulation, hiding internal state behind methods; inheritance, deriving a class from another to reuse behaviour; polymorphism, letting one interface represent different underlying types through overriding and overloading; and abstraction, exposing essential features while hiding implementation. Java supports all four and enforces class-based design.
Q: What is the difference between == and equals()?
A: The == operator compares references for objects, checking whether two variables point to the same object, and compares values for primitives. equals() compares logical content and can be overridden, as String does. So two different String objects with the same text are equal with equals() but not with ==.
Q: Why are Strings immutable in Java?
A: Once created, a String cannot change; any modification produces a new object. Immutability enables safe sharing across threads, allows caching in the string pool, makes strings usable as reliable hash keys and improves security for values like file paths. For heavy modification, use StringBuilder instead.
Q: What is the difference between an interface and an abstract class?
A: An abstract class can have state, constructors and both concrete and abstract methods, and a class extends only one. An interface defines a contract; since Java 8 it can hold default and static methods, and a class can implement many. Use an abstract class for shared base behaviour and an interface for capability.
Q: What is the difference between ArrayList and LinkedList?
A: ArrayList is backed by a dynamic array giving fast random access by index but slower inserts and removals in the middle. LinkedList is a doubly linked list giving fast inserts and deletions at known positions but slow index access. Choose ArrayList for reads and LinkedList for frequent structural changes.
Q: What are checked and unchecked exceptions?
A: Checked exceptions, like IOException, are checked at compile time and must be declared or handled. Unchecked exceptions extend RuntimeException, like NullPointerException, and are not enforced by the compiler. Checked exceptions signal recoverable conditions; unchecked usually indicate programming errors.
Q: What is the difference between final, finally and finalize?
A: final is a modifier that makes a variable constant, a method non-overridable or a class non-extendable. finally is a block that always runs after try or catch for cleanup. finalize was a deprecated method the garbage collector could call before reclaiming an object and should not be relied on.
Intermediate java interview questions
Q: How does a HashMap work internally?
A: A HashMap stores entries in buckets indexed by the key’s hash code. Collisions are chained in a linked list that converts to a balanced tree once a bucket grows large, improving worst-case lookups. It resizes and rehashes when the load factor is exceeded. Correct equals and hashCode implementations are essential for it to work.
Q: Why must equals and hashCode be overridden together?
A: The contract states that equal objects must return the same hash code. If you override equals but not hashCode, two logically equal objects can land in different buckets, so a HashMap or HashSet may fail to find or de-duplicate them. Always override both consistently.
Q: What is the difference between overloading and overriding?
A: Overloading defines multiple methods with the same name but different parameter lists in the same class, resolved at compile time. Overriding redefines a superclass method in a subclass with the same signature, resolved at runtime through dynamic dispatch. Overloading is compile-time polymorphism; overriding is runtime polymorphism.
Q: What is the difference between String, StringBuilder and StringBuffer?
A: String is immutable, so concatenation in a loop creates many objects. StringBuilder is mutable and not thread-safe, giving the best performance for single-threaded building. StringBuffer is mutable and synchronized, making it thread-safe but slower. Prefer StringBuilder unless multiple threads share the buffer.
Q: What is autoboxing?
A: Autoboxing is the automatic conversion between a primitive and its wrapper class, for example int to Integer, and unboxing is the reverse. It lets primitives be stored in collections, but it can cause hidden object creation and NullPointerExceptions when unboxing a null wrapper, so use it consciously.
Q: What does the static keyword mean?
A: A static member belongs to the class rather than any instance, so it is shared across all objects and accessed through the class name. Static methods cannot use instance state or this directly. Static blocks run once when the class loads, which is useful for one-time initialisation.
Q: What is the difference between Comparable and Comparator?
A: Comparable defines a class’s natural ordering through compareTo and is implemented by the class itself. Comparator is a separate object defining an alternative ordering through compare, so you can sort the same type multiple ways without changing it. Use Comparator when you need several sort orders.
Advanced java interview questions
Q: Explain the JVM memory model areas.
A: The main areas are the heap, where objects live and garbage collection occurs; the stack, holding per-thread frames with local variables and call data; the metaspace, storing class metadata; the program counter register per thread; and native method stacks. The heap is shared; stacks and PC registers are per thread.
Q: How does garbage collection work in Java?
A: The collector reclaims objects no longer reachable from GC roots. Modern collectors use generational collection, splitting the heap into young and old generations because most objects die young, which makes minor collections cheap. Collectors such as G1 aim to meet pause-time goals. You cannot force collection; System.gc is only a hint.
Q: What is the difference between a process and a thread?
A: A process is an independent program with its own memory space, while a thread is a lightweight unit of execution within a process that shares the process heap. Threads communicate more easily and cost less to create, but shared memory means you must guard against race conditions.
Q: What does the volatile keyword do?
A: volatile guarantees that reads and writes of a variable go to and from main memory, ensuring visibility of changes across threads and preventing certain reorderings. It does not provide atomicity for compound actions like increment, so it suits simple flags but not counters, which need synchronization or atomic classes.
Q: What is the difference between synchronized and a Lock?
A: synchronized is a built-in monitor that is simple and releases automatically when the block exits. A Lock from java.util.concurrent offers more control — try-lock with timeout, interruptible acquisition and fairness — but you must release it in a finally block. Use Lock when you need those advanced capabilities.
Q: What is the executor framework?
A: The executor framework decouples task submission from thread management using thread pools created by the Executors factory or ThreadPoolExecutor. It reuses threads, bounds concurrency and returns Future or CompletableFuture results. It is preferred over creating raw threads because it handles lifecycle, queuing and resource limits.
Q: What are Java generics and type erasure?
A: Generics provide compile-time type safety for collections and classes, catching type mismatches early and removing casts. Type erasure means the generic type information is removed at runtime and replaced with bounds or Object, which is why you cannot create a generic array directly or check a generic type at runtime.
How to prepare for a Java interview
Drill the OOP pillars, collections internals and the equals-hashCode contract until they are automatic, and code data-structure problems directly in Java rather than pseudocode. Experienced candidates should rehearse concurrency and JVM memory clearly. Keep one project ready to walk through. Pair this with our SQL interview questions for backend roles, and if you are starting out, read the apprenticeship and Skill India guide and check current openings on the GetJobsNews homepage.
Frequently Asked Questions
How do I prepare for a Java interview?
Revise the four OOP pillars, the collections framework, exception handling, multithreading and JVM memory, then code data-structure problems in Java. Understand equals and hashCode, immutability and the string pool, because they appear constantly. Keep one project ready and practise explaining garbage collection and thread safety in plain language.
Is Java hard to learn for freshers?
Java is verbose but very structured, which makes it approachable once you grasp classes and objects. The steep parts are generics, concurrency and the JVM memory model. Because Java enforces types and OOP strictly, freshers who learn the fundamentals well tend to write reliable code and interview confidently.
What Java topics are asked most for freshers?
Freshers face OOP concepts, the difference between an interface and an abstract class, ArrayList versus LinkedList, HashMap internals, String immutability, checked versus unchecked exceptions and the equals versus == distinction. A short coding task on strings, arrays or a simple class design is common.
Do I need to know multithreading for a Java interview?
For freshers, know the basics — threads, the Runnable interface, synchronized and the difference between process and thread. Experienced candidates should understand the executor framework, volatile, locks, the happens-before relationship and common concurrency pitfalls, since backend Java roles routinely test thread safety and performance under load.













