AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

20 Interview idioms / Lesson 6 of 6

The Java theory drill — thirty questions, thirty answers

16 min · +10 XP · 1 exercise · 8 questions

Read the text16 min
Exercise0/1
Quiz8 q

Quick answer

What are the most common Java interview questions?

The most common Java interview questions cover JDK vs JRE vs JVM, whether Java is pass-by-value, why strings are immutable, == vs equals, checked vs unchecked exceptions, overloading vs overriding, abstract classes vs interfaces, how HashMap works, type erasure, ArrayList vs LinkedList, garbage collection, start vs run, synchronized vs volatile, and deadlock. Answer each with a one-sentence definition and one consequence.

Every Java interview has a theory section, and the questions have not changed in a decade. This lesson is the drill: the thirty questions you will be asked, each with the two-sentence answer an interviewer wants to hear and a pointer to the module that explains it in depth. Read it twice a week before interviews. The answers are deliberately compact — the skill being tested is saying the essential thing first, then stopping so the interviewer can ask the follow-up.

Platform and language basics

  1. JDK vs JRE vs JVM? The JVM executes bytecode; the JRE is the JVM plus the standard library; the JDK is the JRE plus compiler and tools. (Module 1)
  2. Why is Java "write once, run anywhere"? Source compiles to platform-neutral bytecode; each platform's JVM interprets and JIT-compiles it. (1)
  3. Is Java pass-by-value or pass-by-reference? Strictly pass-by-value; for objects the value copied is the reference, so callees can mutate the object but not rebind the caller's variable. (5, 16)
  4. Why are strings immutable? Safety (they are everywhere — class names, keys, security), the string pool needs sharing, hash code caching, and thread safety for free. (3, 16)
  5. == vs equals? Reference identity versus logical equality; equals for strings and boxed numbers, always. (3, 20)
  6. final, finally, finalize? Constant/non-overridable/non-extensible; the always-runs block after try; the deprecated GC hook — never use. (7, 12, 16)
  7. Checked vs unchecked exceptions? Checked must be declared or caught (recoverable conditions the caller should handle); unchecked (RuntimeException) signal programming errors. (12)
  8. String, StringBuilder, StringBuffer? Immutable; mutable and fast; mutable and synchronised (legacy). Builder in loops. (3)
  9. Autoboxing pitfalls? Integer cache −128..127 makes == lie; boxing allocates; remove(int) vs remove(Object); unboxing null is an NPE. (2, 16)
  10. static means? Belongs to the class, one copy, no this; static methods cannot be overridden (they hide). (7)

Object orientation

  1. Overloading vs overriding? Same name different parameters, resolved at compile time by static types; same signature in a subclass, dispatched at run time by the object's class. (5, 8)
  2. Abstract class vs interface? A class with state and partial implementation, single inheritance; a contract, multiple implementation, default methods but no instance state. Prefer interfaces; abstract classes for shared state. (8, 9)
  3. Can a constructor be overridden? Called virtually? No — constructors are not inherited; and calling an overridable method from a constructor sees the subclass's fields uninitialised. (7, 8)
  4. What is polymorphism in Java? A reference of a supertype invoking the runtime type's override — dynamic dispatch through the vtable. (8)
  5. Composition vs inheritance? Has-a versus is-a; prefer composition — inheritance couples to the parent's implementation and breaks encapsulation. (8)
  6. What do records give you? Immutable value classes with equals/hashCode/toString/accessors generated; compact constructors for validation. (10)
  7. Sealed classes? A fixed set of permitted subtypes, enabling exhaustive switches. (9, 19)

Generics and collections

  1. Type erasure? Generics exist only at compile time; at run time List<String> is List — no new T[], no instanceof List<String>, one class for all instantiations. (13)
  2. ? extends vs ? super? Producer extends (read), consumer super (write) — PECS. (13)
  3. How does HashMap work? Hash → spread → bucket; chain or tree on collision; resize at 0.75 load; needs consistent equals/hashCode; O(1) average. (14)
  4. ArrayList vs LinkedList? Array-backed, O(1) index, amortised O(1) append; node-backed, O(n) index, poor cache behaviour — almost always ArrayList. (14)
  5. HashMap vs TreeMap vs LinkedHashMap? Unordered O(1); sorted O(log n) with navigation; insertion (or access) order O(1). (14)
  6. Fail-fast iteration? Modifying a collection while iterating throws ConcurrentModificationException; use the iterator's remove or removeIf. (14)
  7. Comparable vs Comparator? Natural order on the type versus an external order per use; Integer.compare, never subtraction. (9, 20)

Streams, memory, concurrency

  1. Intermediate vs terminal operations? Lazy, return a stream, do nothing until a terminal; the terminal runs the pipeline once. Streams are single-use. (15)
  2. Where do objects and locals live? Heap and thread stack respectively; StackOverflowError versus OutOfMemoryError. (16)
  3. How does garbage collection work? Reachability from roots, generational (young/old), stop-the-world pauses reduced by concurrent collectors (G1 default, ZGC). (16)
  4. Thread vs Runnable; start vs run? Pass a Runnable to a Thread; start spawns a thread that calls run; run directly is a plain call. (17)
  5. synchronized vs volatile? Mutual exclusion plus visibility, for compound updates; visibility only, for a flag or a published immutable reference. (17)
  6. What is a deadlock and how do you avoid it? Threads waiting on each other's locks forever; consistent lock ordering, one lock, or tryLock with back-off. (17)

The follow-ups they actually ask

  • After 4: "Then how does StringBuilder exist?" — a separate mutable class; String shares nothing with it.
  • After 9: "So is Integer.valueOf(127) == Integer.valueOf(127) true?" — yes; 128, no.
  • After 11: "What is printed?" — a snippet with an overloaded describe(Animal)/describe(Dog) called with an Animal reference to a Dog: the Animal overload (static type), while a.speak() is the Dog override (runtime type). Fields are never polymorphic.
  • After 18: "Then how does List<String>.get return a String?" — the compiler inserts the cast.
  • After 20: "What if hashCode always returns 1?" — everything in one bucket; O(n) lookups, or O(log n) once it treeifies (Java 8+, if keys are Comparable).
  • After 25: "Can you reuse a stream?" — no; IllegalStateException.
  • After 29: "Is volatile int count; count++ safe?" — no; AtomicInteger.

How to answer theory questions

Lead with the one-sentence definition, add the one consequence that matters, stop. If you know a version number or a class name, say it — "ConcurrentHashMap, since Java 8 with per-bin locking" beats "there's a concurrent map". Never guess a fact you are unsure of; "I believe it is X, but I would check" is a fine answer and a wrong confident answer is not. When you do not know, connect it to what you do: "I have not used StampedLock, but I know ReadWriteLock and would expect it to be the faster, non-reentrant variant."

Key takeaways

  • Thirty questions cover almost every Java theory round; know the two-sentence answer to each.
  • Lead with the definition, add one consequence, stop; name classes and versions.
  • Expect the follow-up: the cache boundary, the overload-versus-override snippet, the all-ones hashCode.
  • Never bluff; connect the unknown to the known.

Common questions

Why are strings immutable in Java?

Because strings are used everywhere — class names, map keys, security checks — immutability makes them safe to share: the string pool can give one object to every user of a literal, the hash code can be cached, and strings pass between threads without synchronisation.

What is the difference between final, finally and finalize in Java?

final makes a variable constant, a method non-overridable or a class non-extensible; finally is the block after try that always runs; finalize is the deprecated garbage-collection hook that should never be used.

How does HashMap work internally in Java?

The key's hashCode is spread and mapped to a bucket; colliding entries share the bucket as a chain, which becomes a tree once it grows long (Java 8+). The table resizes past a 0.75 load factor, and lookups are O(1) on average when equals and hashCode are consistent.

What is the difference between overloading and overriding in Java?

Overloading is one method name with different parameters, chosen at compile time from the arguments' static types. Overriding is a subclass redefining a method with the same signature, chosen at run time from the object's actual class. Fields are never polymorphic.

What is type erasure in Java?

Generic type arguments exist only at compile time: at run time a List<String> is a plain List, so you cannot write new T[] or test instanceof List<String>. The compiler inserts the casts that make get return a String.

What is the difference between String, StringBuilder and StringBuffer?

String is immutable; StringBuilder is mutable and fast; StringBuffer is mutable and synchronised, a legacy class. Use StringBuilder when building a string in a loop, since += copies the whole string each time.

Mark as readSign in to keep your place.

Java Interview Questions and Answers: 30 Core Theory Questions

Thirty core Java interview questions with short answers: JVM, pass-by-value, String immutability, equals and hashCode, HashMap, generics, GC and concurrency.

  • Course: Java study plan
  • Module: Interview idioms
  • Kind: Lesson
  • Reading time: 16 min
  • Runtime: OpenJDK 18

What are the most common Java interview questions?

The most common Java interview questions cover JDK vs JRE vs JVM, whether Java is pass-by-value, why strings are immutable, == vs equals, checked vs unchecked exceptions, overloading vs overriding, abstract classes vs interfaces, how HashMap works, type erasure, ArrayList vs LinkedList, garbage collection, start vs run, synchronized vs volatile, and deadlock. Answer each with a one-sentence definition and one consequence.

Lesson

Every Java interview has a theory section, and the questions have not changed in a decade. This lesson is the drill: the thirty questions you will be asked, each with the two-sentence answer an interviewer wants to hear and a pointer to the module that explains it in depth. Read it twice a week before interviews. The answers are deliberately compact — the skill being tested is saying the essential thing first, then stopping so the interviewer can ask the follow-up.

Platform and language basics

  1. JDK vs JRE vs JVM? The JVM executes bytecode; the JRE is the JVM plus the standard library; the JDK is the JRE plus compiler and tools. (Module 1)
  2. Why is Java "write once, run anywhere"? Source compiles to platform-neutral bytecode; each platform's JVM interprets and JIT-compiles it. (1)
  3. Is Java pass-by-value or pass-by-reference? Strictly pass-by-value; for objects the value copied is the reference, so callees can mutate the object but not rebind the caller's variable. (5, 16)
  4. Why are strings immutable? Safety (they are everywhere — class names, keys, security), the string pool needs sharing, hash code caching, and thread safety for free. (3, 16)
  5. == vs equals? Reference identity versus logical equality; equals for strings and boxed numbers, always. (3, 20)
  6. final, finally, finalize? Constant/non-overridable/non-extensible; the always-runs block after try; the deprecated GC hook — never use. (7, 12, 16)
  7. Checked vs unchecked exceptions? Checked must be declared or caught (recoverable conditions the caller should handle); unchecked (RuntimeException) signal programming errors. (12)
  8. String, StringBuilder, StringBuffer? Immutable; mutable and fast; mutable and synchronised (legacy). Builder in loops. (3)
  9. Autoboxing pitfalls? Integer cache −128..127 makes == lie; boxing allocates; remove(int) vs remove(Object); unboxing null is an NPE. (2, 16)
  10. static means? Belongs to the class, one copy, no this; static methods cannot be overridden (they hide). (7)

Object orientation

  1. Overloading vs overriding? Same name different parameters, resolved at compile time by static types; same signature in a subclass, dispatched at run time by the object's class. (5, 8)
  2. Abstract class vs interface? A class with state and partial implementation, single inheritance; a contract, multiple implementation, default methods but no instance state. Prefer interfaces; abstract classes for shared state. (8, 9)
  3. Can a constructor be overridden? Called virtually? No — constructors are not inherited; and calling an overridable method from a constructor sees the subclass's fields uninitialised. (7, 8)
  4. What is polymorphism in Java? A reference of a supertype invoking the runtime type's override — dynamic dispatch through the vtable. (8)
  5. Composition vs inheritance? Has-a versus is-a; prefer composition — inheritance couples to the parent's implementation and breaks encapsulation. (8)
  6. What do records give you? Immutable value classes with equals/hashCode/toString/accessors generated; compact constructors for validation. (10)
  7. Sealed classes? A fixed set of permitted subtypes, enabling exhaustive switches. (9, 19)

Generics and collections

  1. Type erasure? Generics exist only at compile time; at run time List<String> is List — no new T[], no instanceof List<String>, one class for all instantiations. (13)
  2. ? extends vs ? super? Producer extends (read), consumer super (write) — PECS. (13)
  3. How does HashMap work? Hash → spread → bucket; chain or tree on collision; resize at 0.75 load; needs consistent equals/hashCode; O(1) average. (14)
  4. ArrayList vs LinkedList? Array-backed, O(1) index, amortised O(1) append; node-backed, O(n) index, poor cache behaviour — almost always ArrayList. (14)
  5. HashMap vs TreeMap vs LinkedHashMap? Unordered O(1); sorted O(log n) with navigation; insertion (or access) order O(1). (14)
  6. Fail-fast iteration? Modifying a collection while iterating throws ConcurrentModificationException; use the iterator's remove or removeIf. (14)
  7. Comparable vs Comparator? Natural order on the type versus an external order per use; Integer.compare, never subtraction. (9, 20)

Streams, memory, concurrency

  1. Intermediate vs terminal operations? Lazy, return a stream, do nothing until a terminal; the terminal runs the pipeline once. Streams are single-use. (15)
  2. Where do objects and locals live? Heap and thread stack respectively; StackOverflowError versus OutOfMemoryError. (16)
  3. How does garbage collection work? Reachability from roots, generational (young/old), stop-the-world pauses reduced by concurrent collectors (G1 default, ZGC). (16)
  4. Thread vs Runnable; start vs run? Pass a Runnable to a Thread; start spawns a thread that calls run; run directly is a plain call. (17)
  5. synchronized vs volatile? Mutual exclusion plus visibility, for compound updates; visibility only, for a flag or a published immutable reference. (17)
  6. What is a deadlock and how do you avoid it? Threads waiting on each other's locks forever; consistent lock ordering, one lock, or tryLock with back-off. (17)

The follow-ups they actually ask

  • After 4: "Then how does StringBuilder exist?" — a separate mutable class; String shares nothing with it.
  • After 9: "So is Integer.valueOf(127) == Integer.valueOf(127) true?" — yes; 128, no.
  • After 11: "What is printed?" — a snippet with an overloaded describe(Animal)/describe(Dog) called with an Animal reference to a Dog: the Animal overload (static type), while a.speak() is the Dog override (runtime type). Fields are never polymorphic.
  • After 18: "Then how does List<String>.get return a String?" — the compiler inserts the cast.
  • After 20: "What if hashCode always returns 1?" — everything in one bucket; O(n) lookups, or O(log n) once it treeifies (Java 8+, if keys are Comparable).
  • After 25: "Can you reuse a stream?" — no; IllegalStateException.
  • After 29: "Is volatile int count; count++ safe?" — no; AtomicInteger.

How to answer theory questions

Lead with the one-sentence definition, add the one consequence that matters, stop. If you know a version number or a class name, say it — "ConcurrentHashMap, since Java 8 with per-bin locking" beats "there's a concurrent map". Never guess a fact you are unsure of; "I believe it is X, but I would check" is a fine answer and a wrong confident answer is not. When you do not know, connect it to what you do: "I have not used StampedLock, but I know ReadWriteLock and would expect it to be the faster, non-reentrant variant."

Key takeaways

  • Thirty questions cover almost every Java theory round; know the two-sentence answer to each.
  • Lead with the definition, add one consequence, stop; name classes and versions.
  • Expect the follow-up: the cache boundary, the overload-versus-override snippet, the all-ones hashCode.
  • Never bluff; connect the unknown to the known.

Common questions

Why are strings immutable in Java?

Because strings are used everywhere — class names, map keys, security checks — immutability makes them safe to share: the string pool can give one object to every user of a literal, the hash code can be cached, and strings pass between threads without synchronisation.

What is the difference between final, finally and finalize in Java?

final makes a variable constant, a method non-overridable or a class non-extensible; finally is the block after try that always runs; finalize is the deprecated garbage-collection hook that should never be used.

How does HashMap work internally in Java?

The key's hashCode is spread and mapped to a bucket; colliding entries share the bucket as a chain, which becomes a tree once it grows long (Java 8+). The table resizes past a 0.75 load factor, and lookups are O(1) on average when equals and hashCode are consistent.

What is the difference between overloading and overriding in Java?

Overloading is one method name with different parameters, chosen at compile time from the arguments' static types. Overriding is a subclass redefining a method with the same signature, chosen at run time from the object's actual class. Fields are never polymorphic.

What is type erasure in Java?

Generic type arguments exist only at compile time: at run time a List<String> is a plain List, so you cannot write new T[] or test instanceof List<String>. The compiler inserts the casts that make get return a String.

What is the difference between String, StringBuilder and StringBuffer?

String is immutable; StringBuilder is mutable and fast; StringBuffer is mutable and synchronised, a legacy class. Use StringBuilder when building a string in a loop, since += copies the whole string each time.

Exercise

Static versus dynamic dispatch

Write class Animal { String name = "animal"; String speak() { return "..."; } }, class Dog extends Animal (field name = "dog", speak() returns Woof) and class Cat extends Animal (name = "cat", Meow). Add three static overloads describe(Animal), describe(Dog), describe(Cat) returning describe(Animal), describe(Dog) and describe(Cat). Read n and n words dog/cat; for each, with Animal a = …, print one line:

<word>: speak=<a.speak()> describe=<describe(a)> cast=<describe(…) after a pattern-instanceof cast to the real type> field=<a.name>

Predict before running: the override is chosen at run time, the overload at compile time, and the field by the static type.

Example: 2 then dog cat →

dog: speak=Woof describe=describe(Animal) cast=describe(Dog) field=animal
cat: speak=Meow describe=describe(Animal) cast=describe(Cat) field=animal

In this module: Interview idioms

← Writing clean solutions under time pressure · Final checkpoint — Interview idioms →