Skip to content

Expert · Design patterns, JVM internals and interviews · topic 59 of 60 · 30 min

Java interview questions

Explanation

Java interviews in India and abroad repeat a core set of questions: JDK vs JRE vs JVM, == vs equals, String immutability, HashMap internals, ArrayList vs LinkedList, checked vs unchecked exceptions, final/finally/finalize, abstract class vs interface, overloading vs overriding, why main is static, streams vs collections, and synchronized vs volatile. Answer each in two or three sentences with one example.

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

QuestionShort 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

java
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.
Careful: Do not memorise answers you cannot explain. Interviewers follow up with "why?" and "show me". For every answer in the table, be ready to write a 3–5 line example on the spot.
Think of it like this: Preparing for a Java interview is like preparing for a viva in college: the examiner does not want the textbook recited; they want to see that you can explain an idea simply and apply it to a small example when asked.
Tip: Practise aloud: record yourself answering ten questions on your phone and listen once. You will hear immediately where you ramble or skip the example.

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

Interview questions

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.

java
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)

Task 1 · Question

What does this print? String s1 = "Java"; String s2 = "Ja" + "va"; String s3 = new String("Java").intern(); System.out.println((s1 == s2) + " " + (s1 == s3));

Task 2 · Code · Javascript

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.

Task 3 · Practice

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

Question 1

1.Which is TRUE about finalize()?

Question 2

2.Overriding is resolved…

Question 3

3.Why must a HashMap key override both equals and hashCode?

Question 4

4.System.out.println(10 / 4 + " " + 10 / 4.0) prints…

Read more (free)

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.

From The C# and .NET interview questions asked most often

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.

From C interview questions

What runs Java bytecode?Related

Answer: The Java Virtual Machine (JVM)

Bytecode runs on the JVM.

From What Java is and where it is used

How many interfaces can a class implement?Related

Answer: Many

Multiple interface implementation is allowed.

From Interfaces

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. 1 more topic in “Design patterns, JVM internals and interviews” before its exam: Projects and a Java career.
  2. Module 12 exam: Design patterns, JVM internals and interviews · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. 60 topics left in the course before the final exam.

← Previous: JVM memory and garbage collection