Interview Q&A / Java

INTERVIEW Q&A · 72 QUESTIONS

Java interview questions

Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.

  1. What is the difference between JDK, JRE and JVM?

    The JVM runs Java bytecode. The JRE is the JVM plus the standard libraries needed to run a program. The JDK is the JRE plus developer tools such as the javac compiler, so you need the JDK to write and build Java, and only a runtime to run it.

    BeginnerIn-depth3 min read
  2. Does calling System.gc() force garbage collection in Java?

    No. System.gc() is only a hint, and the JVM may ignore it. Even when it runs, it often triggers an expensive full collection, so production code should not call it.

    BeginnerQuick answer1 min read
  3. What is the difference between heap and stack memory in Java?

    The stack holds method call frames with local primitives and references, and each thread has its own. The heap holds all objects and is shared by every thread and cleaned by the garbage collector.

    BeginnerQuick answer1 min read
  4. What is the difference between OutOfMemoryError and StackOverflowError?

    StackOverflowError happens when a thread call stack is exhausted, usually by endless recursion. OutOfMemoryError happens when the heap, or another memory area, cannot supply memory even after garbage collection.

    BeginnerQuick answer1 min read
  5. When does an object become eligible for garbage collection in Java?

    An object is eligible once no live thread can reach it through any chain of references from the GC roots, such as local variables, static fields and active threads. Setting a reference to null or leaving its scope can make it unreachable.

    BeginnerQuick answer1 min read
  6. How can a memory leak happen in Java even with garbage collection?

    A leak happens when objects are no longer needed but are still reachable, so GC cannot free them. Common causes are growing static collections, unremoved listeners, unclosed resources and keys in a map that are never removed.

    IntermediateQuick answer1 min read
  7. What are the young and old generations in the Java heap?

    New objects start in the young generation, which is collected often and cheaply because most objects die quickly. Objects that survive several collections are promoted to the old generation, which is collected less often.

    IntermediateQuick answer1 min read
  8. What is Metaspace and how is it different from PermGen?

    Metaspace stores class metadata and, since Java 8, replaced PermGen. PermGen was a fixed-size part of the heap, while Metaspace uses native memory and grows as needed unless MaxMetaspaceSize is set.

    IntermediateQuick answer1 min read
  9. What are strong, soft, weak and phantom references in Java?

    A strong reference prevents collection. A soft reference is cleared only when memory is short, a weak reference is cleared at the next collection once no strong refs remain, and a phantom reference is used to know when an object has been reclaimed.

    AdvancedQuick answer1 min read