Course contents

Strings in depth: interview questions & scenarios

Premium16 questions · 17 what-if follow-ups

These are the string questions I ask most, and almost none of them are about method names. A two-line == puzzle or "how many objects?" tells me in a minute whether you can picture the heap, the pool and the bytecode behind a line of Java.

Here's how to use this page: read the question, say your answer out loud first, then open the model answer. The "What if..." follow-ups are the ones I actually ask next. If one stumps you, go back to the lessons: Why String is immutable, How Strings live in memory: the string pool, StringBuilder vs StringBuffer and Interning and compact strings.

Question 1. ConceptWarm-upAsked very often

What's the difference between String, StringBuilder and StringBuffer?

Tests immutability, mutability, synchronization

Show answerHide answer1 follow-up

Model answer

String is immutable: every "change" returns a new object, which makes it safe to share, cache and use as a map key. StringBuilder and StringBuffer are mutable buffers (a byte[] with spare capacity) for building text step by step and calling toString() once at the end.

The only real difference between the two builders is that StringBuffer (Java 1.0) makes every method synchronized, while StringBuilder (Java 5) has no locks. Use StringBuilder by default; strings are almost always built in a local variable on one thread.

How I’d say it in the interview

"String is immutable, so it's thread-safe and great as a key, but modifying it in a loop creates garbage. StringBuilder and StringBuffer are mutable buffers; Buffer synchronizes every method, Builder doesn't. I default to StringBuilder in local variables."

Signals depth

Mentioning that per-method locking doesn't make a sequence of appends atomic, and that on JDK 21 the JIT often elides StringBuffer's locks on a single thread, so the speed gap is small.

Red flags

"StringBuffer is faster" or "StringBuilder is immutable". Also: "always use StringBuffer to be safe".

Follow-ups interviewers ask next

  1. What if I need to compare two StringBuilders by content?

    equals isn't overridden, so sb1.equals(sb2) compares identity. Use sb1.compareTo(sb2) == 0 (Java 11+), "...".contentEquals(sb), or compare their toString() results.

Question 2. What does it print?Warm-upAsked very often

What does this print?

Tests string pool, == vs equals

Show answerHide answer1 follow-up

Model answer

String a = "PAID";
String b = "PAID";
String c = new String("PAID");
String d = c.intern();

System.out.println(a == b);       // true
System.out.println(a == c);       // false
System.out.println(a.equals(c));  // true
System.out.println(a == d);       // true

Both literals resolve to the same pooled object, so a == b. new always creates a fresh object, so a == c is false even though the content matches. intern() returns the pooled instance for "PAID", which is a.

How I’d say it in the interview

"True, false, true, true. Literals share one pooled object, new makes a separate one, equals compares content, and intern hands back the pooled instance. In real code I'd only ever use equals."

Signals depth

Saying the pooled and new objects live in the same heap, and that on Java 9+ c shares a's byte[].

Red flags

"a == c is true because the text is the same." That's confusing reference equality with value equality.

Follow-ups interviewers ask next

  1. What if c came from request.getParameter("status")?

    Then a == c is false too, for the same reason: strings created at runtime are never pooled automatically. That's the production version of this bug.

Question 3. ConceptCoreAsked very often

How many objects does new String("a") create?

Tests ldc, constant pool, heap

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 4. ConceptCoreAsked very often

Why shouldn't you use + inside a loop? Doesn't the compiler optimise it?

Tests O(n²) concatenation, JEP 280

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 5. What does it print?CoreCommonly asked

What does this print, and why?

Tests constant expressions, final, JLS 15.29

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 6. ConceptCoreCommonly asked

Where is the string pool stored, and can pooled strings be garbage collected?

Tests PermGen, heap, weak references

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 7. DesignCoreCommonly asked

Is StringBuffer thread-safe? Would you use one as a shared audit log?

Tests synchronized, compound actions, confinement

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 8. DebuggingCoreCommonly asked

A tenant check passes every unit test, but in production every request is treated as "unknown tenant". Walk me through it.

Tests == on runtime strings, pool

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 9. ScenarioCoreCommonly asked

An export endpoint was fine at launch and now takes seconds as data grew. The profiler shows time in Arrays.copyOf and lots of young GCs. What's happening?

Tests quadratic concatenation, builders, streaming

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 10. ConceptCoreCommonly asked

What does intern() do, and when would you actually use it?

Tests canonicalisation, cost, alternatives

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 11. ConceptSeniorCommonly asked

What are compact strings, and why can one emoji double a string's memory?

Tests JEP 254, coder, Latin-1 vs UTF-16

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 12. ScenarioSeniorCommonly asked

After loading a product catalogue, your service's heap is 70% strings and pods are getting OOM-killed. What do you do?

Tests measurement, dedup, intern, enums

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 13. What does it print?SeniorSometimes asked

What does this print on Java 21?

Tests intern semantics, Java 7 change

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 14. DebuggingSeniorSometimes asked

You changed a constant in a shared library, redeployed the service, and it still uses the old value. How?

Tests constant inlining, binary compatibility

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 15. ScenarioSeniorSometimes asked

We added .intern() to every incoming header value to save memory. P99 latency went up and memory didn't drop. Why?

Tests intern cost, cardinality

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 16. ConceptSeniorSometimes asked

How does javac compile "order-" + id + ":" + qty since Java 9?

Tests invokedynamic, StringConcatFactory, JEP 280

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Premium question set

Keep reading with the Java Pass

The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

From ₹199 · one-time payment · No auto-renewal

Lessons in Strings in depth