Expert · Design patterns, JVM internals and interviews · topic 59 of 60 · 30 min
Java interview questions
Explanation
How to answer
Start with a one-sentence definition, give a tiny example, then one practical consequence. Stop and let the interviewer ask follow-ups. If you do not know, say so and reason from what you do know; confident wrong answers hurt more.
The most asked questions, with crisp answers
| Question | Short answer |
|---|---|
| JDK vs JRE vs JVM? | JVM runs bytecode; JRE = JVM + libraries to run programs; JDK = JRE + tools (javac, jar) to develop. |
| == vs equals? | == compares references (or primitive values); equals compares content when the class overrides it, as String does. |
| Why is String immutable? | Safety and sharing: the string pool, hash-key stability, thread safety and security (file names, URLs cannot change after checks). |
| How does HashMap work? | hashCode picks a bucket in an array; equals finds the key in the bucket; it resizes at load factor 0.75; crowded buckets become trees (Java 8+). |
| ArrayList vs LinkedList? | ArrayList: array-backed, O(1) get, the usual choice. LinkedList: node chain, O(n) get, cheap insert at known positions. |
| Checked vs unchecked exceptions? | Checked (IOException) must be caught or declared; unchecked (RuntimeException) usually signal bugs or bad input. |
| final vs finally vs finalize? | final: cannot change/override/extend; finally: block that always runs after try; finalize: deprecated clean-up hook, do not use. |
| Abstract class vs interface? | Abstract class: shared state and code, single inheritance. Interface: a contract, multiple implementation, default methods. |
| Overloading vs overriding? | Overloading: same name, different parameters, chosen at compile time. Overriding: subclass replaces a method, chosen at run time. |
| Why is main static? | So the JVM can call it without creating an object first. |
| Stream vs collection? | A collection stores data; a stream is a lazy, single-use pipeline that computes over data. |
| synchronized vs volatile? | synchronized gives mutual exclusion and visibility; volatile gives visibility only. |
| What is a record? | A concise immutable data class (Java 16+) with generated constructor, accessors, equals, hashCode and toString. |
| What are virtual threads? | JVM-managed lightweight threads (Java 21) that make blocking I/O cheap at high concurrency. |
Output-prediction favourites
String a = "hi", b = new String("hi");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
Integer x = 127, y = 127, p = 128, q = 128;
System.out.println((x == y) + " " + (p == q)); // true false (Integer cache)
System.out.println(1 + 2 + "3" + 4 + 5); // 3345
System.out.println(10 / 4 + " " + 10 % 4 + " " + 10 / 4.0); // 2 2 2.5
List<Integer> l = new ArrayList<>(List.of(1, 2, 3));
l.remove(1); // removes index 1 → [1, 3]
System.out.println(l);Coding round expectations
- Strings: reverse words, palindrome, anagram, first non-repeating character (HashMap or LinkedHashMap).
- Arrays: two-sum (HashMap), max subarray sum, rotate, remove duplicates from a sorted array.
- Collections and streams: group employees by department, top-N by salary, frequency counts.
- OOP design: parking lot, library management, ATM, with classes, interfaces and a short explanation.
- Spring Boot: design a REST endpoint, explain layers, @Transactional and exception handling.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Java interview questions · 1/6
In this lesson you will practise the Java questions interviewers ask most.
Prefer a video by a teacher? Find free videos on “top java interview questions for freshers hashmap string equals” on YouTube ↗
In real life: A typical service-company interview round
In a 45-minute technical round for a Java developer role at a large IT services company, a fresher was asked: the difference between JDK, JRE and JVM; == vs equals with an example; how HashMap works; checked vs unchecked exceptions; then to write a program counting character frequencies in a string. Short, example-based answers and a clean HashMap solution got her to the next round.
Map<Character, Integer> freq = new LinkedHashMap<>();
for (char c : "banana".toCharArray()) freq.merge(c, 1, Integer::sum);
System.out.println(freq); // {b=1, a=3, n=2}Practice (0/3 done)
What does this print? String s1 = "Java"; String s2 = "Ja" + "va"; String s3 = new String("Java").intern(); System.out.println((s1 == s2) + " " + (s1 == s3));
A classic coding question, in JavaScript so it runs here (the Java version uses a LinkedHashMap the same way): write firstUnique(s) that returns the first character that appears exactly once, or null.
Answer these ten questions ALOUD, each in under one minute with a tiny code example, and record yourself: JDK/JRE/JVM; == vs equals; String immutability; HashMap internals; ArrayList vs LinkedList; checked vs unchecked; final/finally/finalize; abstract class vs interface; overloading vs overriding; synchronized vs volatile.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Projects and a Java careerDesign patterns, JVM internals and interviews · same module
- Common design patternsDesign patterns, JVM internals and interviews · same module
- The C# and .NET interview questions asked most oftenC# and .NET · Interviews, portfolio and careers
- C interview questionsC Programming (from zero to systems) · Embedded C, system calls and careers
- Collections overview and ListThe collections framework
- Maps: HashMap, LinkedHashMap and TreeMapThe collections framework
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which is TRUE about finalize()?
Answer: It is deprecated for removal; use try-with-resources
Finalization is deprecated (JEP 421).
Overriding is resolved…
Answer: At run time
Dynamic dispatch.
Why must a HashMap key override both equals and hashCode?
Answer: So equal keys land in the same bucket and are found
The contract keeps lookups correct.
System.out.println(10 / 4 + " " + 10 / 4.0) prints…
Answer: 2 2.5
Integer division then double division.
const vs readonly: which can be set from a value calculated at run time in the constructor?Related
Answer: readonly
const must be a compile-time literal.
What is the best answer to "what does printf("%d %d", i++, i++) print"?Related
Answer: It is undefined behaviour: i is modified twice without sequencing
Explain and rewrite it.
What runs Java bytecode?Related
Answer: The Java Virtual Machine (JVM)
Bytecode runs on the JVM.
How many interfaces can a class implement?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Design patterns, JVM internals and interviews” before its exam: Projects and a Java career.
- Module 12 exam: Design patterns, JVM internals and interviews · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- 60 topics left in the course before the final exam.