Skip to content
  • Java Interview Q&A · Java

Java Interview Q&A

LearnViaNotes @learnvianotes· 10 min read

The most frequently asked Java interview questions with crisp answers — a final revision sheet.

Platform and basics

What are the JDK, JRE and JVM?

Why is Java platform independent?

Is Java pass-by-value or pass-by-reference?

Java
public class Main {
    static void reassign(StringBuilder sb) { sb = new StringBuilder("new"); }
    static void mutate(StringBuilder sb) { sb.append("!"); }

    public static void main(String[] args) {
        StringBuilder s = new StringBuilder("old");
        reassign(s);
        mutate(s);
        System.out.println(s);
    }
}
Output
old!

What is the difference between == and equals()?

What is the contract between equals() and hashCode()?

Why is String immutable? StringBuilder vs StringBuffer?

final vs finally vs finalize?

Java
public class Main {
    @SuppressWarnings("finally")
    static int tricky() {
        try {
            return 1;
        } finally {
            return 2;          // overrides the try's return (and would swallow exceptions)
        }
    }

    static int counter() {
        int x = 10;
        try {
            return x;          // value 10 is saved now
        } finally {
            x = 20;            // changing x does not change the saved return value
        }
    }

    public static void main(String[] args) {
        System.out.println(tricky() + " " + counter());
    }
}
Output
2 10

OOP

Overloading vs overriding?

Java
public class Main {
    static class Animal {
        static String kind() { return "animal"; }
        String sound() { return "..."; }
    }
    static class Dog extends Animal {
        static String kind() { return "dog"; }          // hides, doesn't override
        @Override String sound() { return "woof"; }
    }

    static void print(Object o) { System.out.println("object"); }
    static void print(String s) { System.out.println("string"); }

    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(a.sound());     // runtime type decides
        System.out.println(Animal.kind() + " " + Dog.kind());
        Object o = "text";
        print(o);                          // compile-time type decides
        print("text");
    }
}
Output
woof
animal dog
object
string

Abstract class vs interface?

What are the four pillars of OOP in Java?

Static vs instance members?

Records vs regular classes?

What are sealed classes for?

Exceptions

Checked vs unchecked exceptions?

What does try-with-resources do?

Collections

How does HashMap work internally?

HashMap vs Hashtable vs ConcurrentHashMap?

Fail-fast vs fail-safe iterators?

Java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4));
        try {
            for (Integer n : nums) {
                if (n % 2 == 0) nums.remove(n);      // structural change during for-each
            }
        } catch (ConcurrentModificationException e) {
            System.out.println("ConcurrentModificationException");
        }
        nums = new ArrayList<>(List.of(1, 2, 3, 4));
        nums.removeIf(n -> n % 2 == 0);              // the safe way
        System.out.println(nums);
    }
}
Output
ConcurrentModificationException
[1, 3]

Comparable vs Comparator?

ArrayList vs LinkedList?

Concurrency

volatile vs synchronized vs atomic classes?

wait() vs sleep()?

Runnable vs Callable?

What are virtual threads?

What is a deadlock and how do you prevent it?

JVM and memory

What are the JVM memory areas?

How does garbage collection decide what to free?

What is the difference between a ClassNotFoundException and a NoClassDefFoundError?

Functional Java

What is a functional interface?

Why are streams called lazy?

Java
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream<Integer> pipeline = Stream.of(1, 2, 3, 4, 5)
                .filter(n -> { System.out.println("filter " + n); return n % 2 == 0; })
                .map(n -> { System.out.println("map " + n); return n * 10; });
        System.out.println("nothing has run yet");
        System.out.println("first = " + pipeline.findFirst().get());
    }
}
Output
nothing has run yet
filter 1
filter 2
map 2
first = 20

How should Optional be used?

map() vs flatMap() in streams?

Rapid-fire tricks

Java
public class Main {
    public static void main(String[] args) {
        System.out.println(10 / 3 + " " + 10 % 3 + " " + 10 / 3.0);
        System.out.println((byte) 200);                 // narrowing wraps
        char c = 'A';
        c += 1;                                         // compound assignment casts implicitly
        System.out.println(c);
        int i = 5;
        i = i++ + ++i;                                  // 5 + 7
        System.out.println(i);
        System.out.println(Integer.MAX_VALUE + 1);
        System.out.println(1.0 / 0 + " " + (0.0 / 0 == 0.0 / 0));
        String s = null;
        System.out.println(s + "x");                    // concatenation turns null into "null"
    }
}
Output
3 1 3.3333333333333335
-56
B
12
-2147483648
Infinity false
nullx

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report