Java: Interview questions: core Java, collections, concurrency and Spring
Java interviews usually start with OOP and core language questions, move to collections and concurrency, then add a few Java 8 and Spring or JDBC questions and one or two coding problems. This kit works for any employer.
21 questions · 3 rounds · updated Nov 2026
How the interview works
- Core Java and OOP: Keywords, strings, exceptions, memory.
- Collections and concurrency: HashMap internals, threads, synchronisation.
- Modern Java and practical: Streams, Optional, Spring basics, short coding tasks.
Sample questions
- 01
Why is String immutable in Java? How do String, StringBuilder and StringBuffer differ?
MediumStrings
Model answer
What the interviewer checks
Real reasons (string pool, security, hashing, thread safety) and when to use each class.
How to answer
String is immutable because: • String pool: literals are shared. If one reference could change "admin", every other reference would see it. • Security: class names, file paths and URLs are strings, and couldn't be trusted if callers could change them after a check. • Hashing: String caches its hashCode, so it is a fast, stable HashMap key. • Thread safety: immutable objects can be shared without locks. The three classes: • String: immutable. `s + x` inside a loop creates a new object each time, O(n²) for n appends. • StringBuilder: mutable, not synchronised, and fastest. Use it for building strings in a single thread. • StringBuffer: mutable and synchronised. A legacy choice, rarely needed today.
Likely follow-ups
- `String a = "hi"; String b = new String("hi");` → `a == b` is false (different objects), `a.equals(b)` is true, and `a == b.intern()` is true.
- 02
String a = "java"; String b = "ja" + "va"; String c = new String("java"); Which are true?
MediumStrings
- 03
Write a Java method that counts the frequency of each word in a sentence, case-insensitively, and returns the words sorted by count (highest first).
EasyCoding
Approach and solution
Approach
Split on non-letters, lower-case, count into a HashMap with merge(), then sort the entries by value in descending order. Mention that a stream with groupingBy and counting does the same thing.
import java.util.*; import java.util.stream.*; public class WordCount { static List<Map.Entry<String, Integer>> count(String text) { Map<String, Integer> freq = new HashMap<>(); for (String w : text.toLowerCase().split("[^a-z]+")) { if (!w.isEmpty()) freq.merge(w, 1, Integer::sum); } return freq.entrySet().stream() .sorted(Map.Entry.<String, Integer>comparingByValue().reversed() .thenComparing(Map.Entry.comparingByKey())) .collect(Collectors.toList()); } public static void main(String[] args) { System.out.println(count("The cat and the hat. The END and")); // [the=3, and=2, cat=1, end=1, hat=1] } }Complexity: O(n) to count and O(k log k) to sort k distinct words.
18 more questions, members only
- Core Java and OOP10
- Collections and concurrency5
- Modern Java and practical6
Members also get how to answer each question and a preparation plan for this company.
See plans from ₹399Every question and answer is written by Hirecept and the code is run before it is published. How a company runs its process comes from candidate reports and public pages, and companies change it, so treat that part as a guide, not a promise.