Java String Interview Questions 2026: 27 Q&A With Code

What changed in 2026 drives
Mass-recruiter offer letters are flatter for 2026 batch - the 4-5 LPA ASE band has barely budged in three years while inflation eats real wages. Premium tracks (Digital, Pro, Elite, Specialist) are still where the differential lives, and they are entirely test-driven. If you are aiming higher than the default offer, the coding round is not optional pageantry - it is the entire interview.
What I'd actually study for this
- 01Two solid coding-round answers (1 medium-hard DSA each, with edge-case discussion) > five half-baked ones
- 02One real project you can defend end-to-end - file paths, design decisions, and what you would change
- 03One DBMS schema you actually built (not a textbook ER diagram), with at least 3 join-heavy queries written from memory
- 04Three behavioural STAR stories: failure recovered, conflict handled, ownership taken
Where most candidates trip up
The single biggest mistake is treating company-specific guides as primary prep and DSA as secondary. It is the opposite. Mass recruiters use the test as a filter, but premium tracks at every IT services company use coding to allocate offer band. Spend 70% of prep time on DSA + system fundamentals, 20% on company-specific patterns, 10% on HR rehearsal. Reverse that ratio and you collect the default offer.
Editorial commentary by Aditya Sharma · written for PapersAdda · not generated, not aggregated.
Last Updated: June 2026 | Level: Freshers to Mid-Level | Read Time: ~15 min
Strings appear in nearly every Java interview because they touch immutability, memory, hashing, and performance all at once. Candidates report that the string pool, == versus equals, and StringBuilder versus StringBuffer are guaranteed questions. This guide covers 27 String questions with full answers, runnable code, and predict-the-output traps. Behaviour reflects the official Oracle java.lang.String documentation; confirm any method-level detail on the official docs.
Pair this with Java Interview Questions 2026 and Java Collections Interview Questions 2026.
Table of Contents
- Immutability and the Pool (Q1 to Q9)
- Comparison and Methods (Q10 to Q18)
- StringBuilder and Performance (Q19 to Q24)
- Advanced (Q25 to Q27)
- Predict the Output
- Quick Revision Table
- Frequently Asked Questions
Immutability and the Pool
Q1. Why is String immutable in Java? Medium
Q2. What is the string constant pool? Medium
String a = "hi"; // goes to pool
String b = "hi"; // reuses the same pool reference
System.out.println(a == b); // true
Q3. What is the difference between String literal and new String()? Medium
String x = new String("hi");
System.out.println(x == "hi"); // false
System.out.println(x.intern() == "hi"); // true
Q4. What does the intern() method do? Hard
Q5. How many objects are created by String s = new String("hi")? Hard
Q6. Is String thread-safe? Medium
Q7. Can you extend the String class? Easy
Q8. Why is String a popular choice for HashMap keys? Medium
Q9. What is the size of the string pool and where does it live? Hard
Comparison and Methods
Q10. What is the difference between == and equals for strings? Easy
Q11. What is the difference between equals and equalsIgnoreCase? Easy
Q12. What does compareTo return for strings? Medium
Q13. What is the difference between substring in older and newer Java? Hard
Q14. How do you reverse a string in Java? Easy
String reversed = new StringBuilder("abc").reverse().toString(); // "cba"
Q15. How do you check if two strings are anagrams? Medium
boolean anagram(String a, String b) {
if (a.length() != b.length()) return false;
int[] count = new int[256];
for (char c : a.toCharArray()) count[c]++;
for (char c : b.toCharArray()) if (--count[c] < 0) return false;
return true;
}
Q16. What does String.split do with a trailing empty string? Hard
Q17. What is the difference between length() for String and length for arrays? Easy
Q18. How do you convert a String to a char array and back? Easy
StringBuilder and Performance
Q19. What is the difference between String, StringBuilder, and StringBuffer? Medium
| Type | Mutable | Thread-safe | Speed |
|---|---|---|---|
| String | No | Yes (immutable) | n/a |
| StringBuilder | Yes | No | Fast |
| StringBuffer | Yes | Yes | Slower |
Q20. Why is string concatenation in a loop inefficient? Medium
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) sb.append(i);
String result = sb.toString();
Q21. How does the compiler optimise simple string concatenation? Hard
Q22. Is StringBuilder thread-safe? Easy
Q23. How do you efficiently join a list of strings? Medium
String csv = String.join(",", List.of("a", "b", "c")); // "a,b,c"
Q24. What are text blocks in Java? Medium
Advanced
Q25. How does the JVM handle String concatenation with primitives? Medium
Q26. What is the hashCode formula for String? Hard
Q27. What is the difference between isEmpty and isBlank? Easy
Predict the Output
Snippet 1
String a = "java";
String b = "ja" + "va"; // compile-time constant
String c = new String("java");
System.out.println(a == b);
System.out.println(a == c);
Output:
true
false
Why: "ja" + "va" is folded into the literal "java" at compile time and interned, so a == b is true. new String creates a distinct heap object, so a == c is false.
Snippet 2
String s = "abc";
s.concat("def");
System.out.println(s);
Output:
abc
Why: Strings are immutable, concat returns a new string that is discarded here. s is unchanged. You must assign the result.
Snippet 3
System.out.println("5" + 3 + 2);
System.out.println(5 + 3 + "2");
Output:
532
82
Why: Evaluation is left to right. In line 1, the first operand is a string, so everything concatenates. In line 2, 5 + 3 computes 8 first, then concatenates with "2".
Quick Revision Table
| Concept | Key takeaway |
|---|---|
| Immutability | Enables pool, thread safety, safe keys |
| == vs equals | reference vs content |
| Literal vs new | pooled vs fresh heap object |
| Loop concat | use StringBuilder, not + |
| StringBuilder vs Buffer | unsynchronised vs synchronised |
| substring leak | fixed since Java 7u6 |
| hashCode | 31-based polynomial, cached |
Frequently Asked Questions
Why is String immutable in Java?
Immutability enables safe string-pool sharing, thread safety without locks, safe use as HashMap keys with a cached hash code, and security for sensitive values like file paths. It is the single most asked String concept.
What is the difference between == and equals for strings?
== compares references, equals compares character content. Because of the string pool, == can be accidentally true for literals but breaks for new String. Always use equals for value comparison.
When should I use StringBuilder over String concatenation?
Use StringBuilder whenever you build a string in a loop. Repeated + creates a new String each iteration giving O(n squared) behaviour, while StringBuilder mutates one buffer in O(n).
How do I reverse a string in an interview?
The one-liner is new StringBuilder(s).reverse().toString(), but be ready to do it manually with a two-pointer swap on a char array, since some interviewers want to see the mechanics rather than the library call.
What is the difference between isEmpty and isBlank?
isEmpty is true only for a zero-length string, while isBlank (Java 11+) is also true for strings that contain only whitespace. Use isBlank to treat " " as effectively empty.
Related Articles
- Java Interview Questions 2026, the master 50-question set
- Java Collections Interview Questions 2026, hashing and keys
- Java OOPs Interview Questions 2026, OOP foundations
- Java Output Prediction Questions 2026, more tricky snippets
Confirm any method-level behaviour on the official Oracle java.lang.String documentation before your interview. This guide reflects candidate-reported patterns and public preparation resources as of June 2026.
Methodology applied to this articlelast verified 8 Jun 2026
- No fabricated salary numbers or success rates. If we quote a range, it's sourced.
- No noun-substituted templates. This article was not generated by swapping company names in a stock prompt.
- No paid placements, sponsored coaching links, or affiliate-shilled course pushes.
Explore this topic cluster
More resources in Interview Questions
Use the category hub to browse similar questions, exam patterns, salary guides, and preparation resources related to this topic.
Paid contributor programme
Sat this this year? Share your story, earn ₹500.
First-person experience reports help future candidates prep smarter. We pay verified contributors ₹500 via UPI per accepted story - with byline.
Submit your story →Ready to practice?
Take a free timed mock test
Put what you learned into practice. Our mock tests match the 2026 pattern with timer, navigator, reveal, and score breakdown. No signup.
Start Free Mock Test →Related Articles
Airbnb Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing Airbnb's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical, behavioural,...
Airtel Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing Airtel's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical, behavioural,...
AMD Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing AMD's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical, behavioural,...
Atlassian Interview Questions 2026: Top Tech, HR & Behavioural Q&As for Freshers
Clearing Atlassian's fresher loop in 2026 comes down to preparing for the exact mix of questions across technical,...
Barclays Interview Questions 2026
_Last verified by [Aditya Sharma](/author/aditya-sharma/) · cross-checked against PapersAdda Hiring Pulse and...
More from PapersAdda
Accenture Interview Process 2026: Rounds & Prep
Accenture Interview Questions 2026 (with Answers for Freshers)
Adobe Interview Process 2026: Rounds, OA & Aptitude
Amazon Interview Process 2026: Full Loop + Bar Raiser