Interview Preparation· 60 min

Core and Advanced Java Interview Questions

Revise language, collections, JVM, exceptions, concurrency, and performance questions.

Java interview lesson
Question 33 · Not reviewed

Interview prep · core Java revision

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Core and Advanced Java Interview Questions

Explain it aloud in your own words

Strong answer

Core Java rounds probe whether you understand why the language behaves as it does, not whether you can recite definitions. Expect questions that cross topics: a HashMap question becomes a concurrency question, an immutability question becomes a memory-model question, and a stream question becomes a performance question. The strongest answers state the rule, show a short example, name the failure mode, and say how you would detect it in a real codebase.

Key points

  • Revise the contracts most often tested: equals/hashCode, Comparable consistency, and immutability.
  • Know HashMap internals: buckets, resize, treeification of long bins, and why it is not thread-safe.
  • For concurrency, always name the missing guarantee: atomicity, visibility, or ordering.
  • Back JVM claims with tools you have used: jcmd, JFR, thread dumps, heap dumps.

Interviewer looks for

Interviewers push one level deeper after each answer. Show that you can move from definition to internals to a production consequence without guessing.

Java example

1// Classic trap: what does this print, and why?
2Integer a = 127, b = 127;
3Integer c = 128, d = 128;
4System.out.println(a == b); // true  - Integer cache (-128..127)
5System.out.println(c == d); // false - different boxed objects
6System.out.println(c.equals(d)); // true
7
8// Follow-up: make this thread-safe without a global lock
9Map<String, LongAdder> hits = new ConcurrentHashMap<>();
10void record(String path) {
11  hits.computeIfAbsent(path, k -> new LongAdder()).increment();
12}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

What happens internally when you put a key into a HashMap?

Strong answer

The key's hashCode is spread and masked to choose a bucket. If the bucket is empty the entry is stored; otherwise keys are compared with equals and the value is replaced or a new node is chained. Long chains are converted to balanced trees, and the table doubles when the size exceeds capacity times load factor.

Q2Basic

Why is the Integer comparison 128 == 128 false while 127 == 127 is true?

Strong answer

Autoboxing uses Integer.valueOf, which caches instances from -128 to 127. Values in that range share the same object, so == compares equal references; outside it, new objects are created. Always use equals for boxed values.

Q3Intermediate

What is the difference between fail-fast and fail-safe iterators?

Strong answer

Fail-fast iterators, such as those of ArrayList and HashMap, detect structural modification through a modCount and throw ConcurrentModificationException on a best-effort basis. Iterators of concurrent collections are weakly consistent or snapshot-based, so they tolerate modification but may not reflect the latest changes.

Q4Intermediate

How would you design an immutable class in Java?

Strong answer

Make the class final or control subclassing, make fields private and final, validate in the constructor, defensively copy mutable inputs and outputs, and expose no mutators. Immutable objects are thread-safe once safely published and make good map keys.

Q5Advanced

How would you investigate a Java service whose memory keeps growing until it restarts?

Strong answer

Confirm whether it is heap, metaspace, or native memory using GC logs, metrics, and Native Memory Tracking. For heap growth, take heap dumps at intervals and compare dominators and retained sizes to find what keeps objects reachable, such as an unbounded cache, listener, or ThreadLocal. Then fix the retention and verify with the same measurement.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.