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