CCareerClutch
← All posts
·6 min read·CareerClutch Team

Java Interview Questions for Freshers in India (with Sample Answers)

interview-preptechnicaljava

If you're sitting for a campus placement at a service company — TCS, Infosys, Wipro, Cognizant — or applying for a junior developer role, a Java round is almost guaranteed. The questions are not as hard as they look. They test whether you understand the why behind syntax, not just the syntax itself.

Here are the questions that appear most often, with answers you can actually use.

The Core OOP Questions

1. What is the difference between a class and an object?

A class is a blueprint; an object is a thing built from that blueprint.

"A class is like the design drawings for a house. An object is an actual house built from those drawings. You can build many houses from one set of drawings — each with its own address, colour, and furniture."

Interviewers love this question because weak candidates confuse syntax (writing class Car) with the concept (what a class is).

2. Explain the four pillars of OOP.

  • Encapsulation — bundling data and methods together, hiding internal state (private fields + getters/setters).
  • Abstraction — showing only what's necessary; hiding how it works (interfaces, abstract classes).
  • Inheritance — a child class gets the behaviour of a parent class (extends).
  • Polymorphism — the same method name behaves differently depending on context (method overloading = compile-time; method overriding = runtime).

Give a one-sentence definition for each. You don't need paragraphs — interviewers want precision, not essays.

3. What is the difference between method overloading and method overriding?

Overloading happens in the same class: same method name, different parameters. Resolved at compile time.

Overriding happens in a child class: same method name and same parameters. The child provides its own implementation. Resolved at runtime.

// Overloading — same class, different parameters
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

// Overriding — child class replaces parent's method
class Animal { void speak() { System.out.println("..."); } }
class Dog extends Animal {
    @Override void speak() { System.out.println("Woof"); }
}

Java-Specific Concepts

4. What is the JVM, JDK, and JRE — and how are they different?

  • JVM (Java Virtual Machine): the engine that runs compiled Java bytecode. It's what makes Java "write once, run anywhere."
  • JRE (Java Runtime Environment): JVM + the standard libraries needed to run a Java program.
  • JDK (Java Development Kit): JRE + development tools (compiler javac, debugger) needed to write and compile Java.

Think of nesting dolls: JDK ⊃ JRE ⊃ JVM.

5. What is the difference between == and .equals() in Java?

== compares references (memory addresses). .equals() compares content.

String a = new String("hello");
String b = new String("hello");
System.out.println(a == b);       // false — different objects in memory
System.out.println(a.equals(b));  // true  — same content

This question trips up many freshers. Memorise the example above — it's the clearest illustration.


Strings

6. Why is String immutable in Java?

Once a String is created, you can't change its characters. When you "modify" a string, Java creates a new object in memory.

Immutability enables the String Pool: Java caches string literals so multiple variables pointing to the same value share one object. This saves memory and makes strings safe to use as HashMap keys (because the hash code can't change after insertion).

7. When should you use StringBuilder instead of String?

Use String when the value won't change.

Use StringBuilder when you're building a string in a loop (e.g. concatenating 100 items). String concatenation in a loop creates a new object on every iteration — O(n²) total. StringBuilder modifies a buffer in place — O(n).

StringBuffer is the thread-safe version of StringBuilder. If thread safety isn't a concern, StringBuilder is faster.


Collections and Data Structures

8. What is the difference between ArrayList and LinkedList?

| | ArrayList | LinkedList | |---|---|---| | Backed by | Dynamic array | Doubly linked list | | Random access | O(1) | O(n) | | Insert/delete (middle) | O(n) | O(1) | | Typical use | Default list choice | Frequent insertions/deletions |

For most use cases, ArrayList is the right default. Prefer LinkedList only when you need frequent insertions or deletions in the middle of a large list.

9. How does a HashMap work internally?

A HashMap stores key-value pairs in an array of buckets. When you call put(key, value):

  1. Java computes key.hashCode() and maps it to a bucket index.
  2. If two keys land in the same bucket (hash collision), they're stored in a linked list (or a balanced tree for many collisions) at that bucket.
  3. get(key) repeats the hash computation and walks that bucket's chain to find the entry.

This is why HashMap is O(1) average, and why overriding hashCode() correctly matters when you use custom objects as keys.


Exception Handling

10. What is the difference between checked and unchecked exceptions?

  • Checked exceptions are verified at compile time. You must handle them with try-catch or declare them with throws. Examples: IOException, SQLException.
  • Unchecked exceptions (subclasses of RuntimeException) are not checked at compile time. Examples: NullPointerException, ArrayIndexOutOfBoundsException.

The rule of thumb: checked exceptions represent recoverable conditions (file not found — try again); unchecked exceptions represent programming bugs (null pointer — fix the code).


Interfaces vs. Abstract Classes

11. What is the difference between an interface and an abstract class?

| | Interface | Abstract class | |---|---|---| | Methods | Abstract by default (Java 8+ allows default methods) | Mix of abstract and concrete | | Fields | Only public static final constants | Any field type | | Multiple inheritance | A class can implement multiple interfaces | A class can extend only one | | When to use | Define a capability (Comparable, Runnable) | Define a base type with shared code |


A note on interview strategy

For a fresher Java round, you will rarely be asked to write complex algorithms from scratch. Most questions are conceptual. The trap is giving a textbook definition without showing you understand it.

Back every definition with a quick example or analogy. "Encapsulation means hiding data — like a vending machine that shows you buttons but hides the mechanism inside." That one sentence separates a memorised answer from a real one.

Tools like CareerClutch ask you these questions out loud and give you specific feedback on which explanations landed clearly versus which fell flat — so you know exactly what to fix before the real interview.

Prepare 10 of these questions thoroughly rather than skimming 50. Depth beats breadth in a technical interview every time.