Java Core interview questions
54 Java Core interview questions with answers, from junior to mid level.
Foundations
- What is Java?
- What happens between writing Java code and running it?
- What is the JVM (Java Virtual Machine)?
- What do you install to write, compile and run Java programs?
- What does LTS mean for Java versions, and which versions are LTS?
- In Java, what are a class and an object?
- In a Java class, what are fields and methods?
- What does this code do?
- What is
public static void main(String[] args)? - What are a package and an
importin Java? - What is a JAR file?
- What is a Java interface?
- What does
nullmean in Java, and what is a NullPointerException? - What is an IDE?
Core
- What is the difference between the JDK, the JRE and the JVM?
- "Write once, run anywhere" in Java is possible mainly because:
- Which statement about
intandIntegeris correct? - What does this print?
- Why should you avoid
s += xfor building a long string inside a big loop? - What does the keyword
staticmean on a method or field? - What does
finaldo? - Which access modifier is used when you write NO keyword in front of a field or method?
- What is the main difference between an interface and an abstract class in Java?
- Which collection should you choose to store unique items with no duplicates?
- In practice, which is the better default for a general-purpose list:
ArrayListorLinkedList? - Which of these is a CHECKED exception?
- What is the difference between method overloading and overriding?
- Explain the four pillars of OOP with Java examples.
Intermediate
- If you override
equals()in a class, what else must you override, and why? - What does this print?
- What is
try-with-resourcesfor? - What is type erasure in Java generics?
- What does this print?
- What does this print?
- What is the recommended way to use
Optional<T>? - What do records (Java 16+) give you with
record Point(int x, int y) {}? - Where do objects live in the JVM, and what lives on the stack?
- What does this print?
- Why does this code throw
ConcurrentModificationException? - Which is true about
HashMap,HashtableandConcurrentHashMap? - What is the difference between
synchronizedandvolatile? - Why use an
ExecutorService(thread pool) instead ofnew Thread()for every task? - When would you use
Comparatorinstead ofComparable? - Explain how a HashMap works internally.
- Explain the Java memory model: heap, stack, metaspace and garbage collection.
- What changed in modern Java (8 to 21) that every developer should know?
Advanced
- Java has a garbage collector. How can a Java program still have a memory leak?
- What problem do virtual threads (Java 21) solve?
- Why is this counter NOT thread-safe even though the field is
volatile? - What is the practical difference between
thenApplyandthenComposeinCompletableFuture? - How do you make a class truly immutable in Java?
- A class overrides
equalsbut NOThashCode. What is the likely result? - What does a "stop-the-world" pause in garbage collection mean, and how do modern collectors reduce it?
- What concurrency tools does Java give you, and when do you use each?