JavaInterviewFreshersOOPCollections
Top 50 Java Interview Questions for Freshers 2026 — With Answers
Siva Prasad Galaba· Staff Engineer & Founder, CodeBegun·
The most commonly asked Java interview questions for freshers in 2026, with clear answers. Covers OOP, Collections, Strings, Exception Handling, Multithreading, and Java 8+.
## Java Interview Questions for Freshers 2026
These are the questions our students have reported from actual interviews at TCS, Infosys, Wipro, EPAM, Mphasis, and product startups in Hyderabad. Prepared by CodeBegun faculty with 15+ years of industry experience.
---
## Section 1 — OOP Concepts
### 1. What are the four pillars of OOP?
**Encapsulation** — wrapping data and methods into a class, restricting direct access using access modifiers.
**Inheritance** — a child class acquires properties and methods from a parent class (`extends`).
**Polymorphism** — same method name, different behavior. Compile-time (method overloading) and runtime (method overriding).
**Abstraction** — hiding implementation details and exposing only the interface. Achieved via abstract classes and interfaces.
### 2. What is the difference between an abstract class and an interface?
| Feature | Abstract Class | Interface |
|---|---|---|
| Instantiation | Cannot be instantiated | Cannot be instantiated |
| Methods | Can have abstract + concrete | Default + abstract (Java 8+) |
| Variables | Can have instance variables | Only `public static final` |
| Constructor | Yes | No |
| Multiple inheritance | No (single only) | Yes (a class can implement many) |
| Use case | IS-A with shared state | Contract (HAS-A capability) |
### 3. What is method overloading vs method overriding?
**Overloading** — same method name, different parameters, in the same class. Resolved at compile time.
```java
void print(String s) {}
void print(int n) {} // overloaded
```
**Overriding** — subclass provides a different implementation for a parent class method. Same signature. Resolved at runtime.
```java
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Woof"); }
}
```
### 4. What is the difference between `==` and `.equals()` in Java?
`==` compares **references** (memory addresses) for objects. For primitives, it compares values.
`.equals()` compares **content**. `String`, `Integer`, and other wrapper classes override it to compare values.
```java
String a = new String("hello");
String b = new String("hello");
a == b // false (different objects)
a.equals(b) // true (same content)
```
### 5. What is the `final` keyword used for?
- **`final` variable** — value cannot be changed (constant)
- **`final` method** — cannot be overridden in a subclass
- **`final` class** — cannot be subclassed (e.g., `String`, `Integer`)
---
## Section 2 — Java Collections
### 6. What is the difference between ArrayList and LinkedList?
| Feature | ArrayList | LinkedList |
|---|---|---|
| Backed by | Dynamic array | Doubly linked list |
| Random access (get) | O(1) | O(n) |
| Insertion/deletion (middle) | O(n) | O(1) |
| Memory | Less overhead | More (stores prev/next pointers) |
| Use case | Frequent reads | Frequent insertions/deletions |
### 7. What is the difference between HashMap and HashTable?
| Feature | HashMap | HashTable |
|---|---|---|
| Thread safety | Not thread-safe | Thread-safe (synchronized) |
| Null keys | One null key allowed | No null keys |
| Performance | Faster | Slower (synchronized methods) |
| Iteration | Uses Iterator | Uses Enumerator |
| Modern alternative | `ConcurrentHashMap` for thread-safety | Deprecated in new code |
### 8. What is the difference between HashMap, LinkedHashMap, and TreeMap?
- **HashMap** — no ordering guaranteed, O(1) get/put
- **LinkedHashMap** — maintains insertion order, O(1) get/put
- **TreeMap** — sorted by natural order or custom Comparator, O(log n) get/put
### 9. What is the difference between `Iterator` and `ListIterator`?
- **Iterator** — forward traversal only, works on all Collection types
- **ListIterator** — bidirectional (forward + backward), works only on `List`, can add/set elements
### 10. What happens if you modify a Collection while iterating over it?
You get a `ConcurrentModificationException`. Use `Iterator.remove()` instead of `collection.remove()` during iteration, or use `CopyOnWriteArrayList` for concurrent scenarios.
---
## Section 3 — Strings
### 11. Why is String immutable in Java?
Once created, a `String` object's value cannot be changed. Any modification creates a **new String** object.
Reasons for immutability:
1. **Security** — used in network connections, file paths, class loading
2. **Thread safety** — immutable objects are safe without synchronization
3. **String Pool** — allows JVM to reuse string literals safely
4. **HashCode caching** — constant hash allows use as HashMap keys
### 12. What is the String Pool?
A special memory region in the JVM heap where string literals are stored. When you write `String s = "hello"`, Java checks the pool first. If `"hello"` exists, it returns the same reference. If not, it creates a new entry.
`new String("hello")` always creates a new object, bypassing the pool.
### 13. What is the difference between String, StringBuilder, and StringBuffer?
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread safety | Yes (immutable) | No | Yes (synchronized) |
| Performance | Slowest for concat | Fastest | Slower than StringBuilder |
| Use case | Constants, small concat | Single-thread concat | Multi-thread concat |
---
## Section 4 — Exception Handling
### 14. What is the difference between Checked and Unchecked exceptions?
**Checked exceptions** — must be handled with try-catch or declared with `throws`. Examples: `IOException`, `SQLException`, `ClassNotFoundException`.
**Unchecked exceptions** — extend `RuntimeException`. No mandatory handling. Examples: `NullPointerException`, `ArrayIndexOutOfBoundsException`, `IllegalArgumentException`.
### 15. What is the difference between `throw` and `throws`?
- **`throw`** — used inside a method to explicitly throw an exception object
- **`throws`** — used in method signature to declare that it might throw an exception
```java
void readFile(String path) throws IOException {
if (path == null) throw new IllegalArgumentException("Path is null");
// ...
}
```
### 16. What is the `finally` block and when does it NOT execute?
`finally` always executes after try-catch, whether or not an exception occurred. Used for cleanup (closing connections, files).
**Does NOT execute** when `System.exit()` is called or when the JVM crashes (e.g., `OutOfMemoryError`).
### 17. Can you catch multiple exceptions in one catch block?
Yes, since Java 7:
```java
try {
// risky code
} catch (IOException | SQLException e) {
// handle both
}
```
---
## Section 5 — Java 8+ Features
### 18. What are lambda expressions?
Anonymous functions that implement a functional interface. Syntax: `(parameters) -> expression`
```java
List<String> names = Arrays.asList("Priya", "Ravi", "Sai");
names.forEach(name -> System.out.println(name));
// or with method reference:
names.forEach(System.out::println);
```
### 19. What is the Stream API?
A sequence of elements supporting sequential and parallel aggregate operations. Streams don't store data — they pipeline operations on a data source.
```java
List<Integer> result = numbers.stream()
.filter(n -> n > 10)
.map(n -> n * 2)
.collect(Collectors.toList());
```
### 20. What is Optional in Java 8?
A container that may or may not contain a non-null value. Avoids `NullPointerException`.
```java
Optional<String> name = Optional.ofNullable(getName());
name.ifPresent(n -> System.out.println(n));
String value = name.orElse("Unknown");
```
### 21. What are default methods in interfaces?
Methods with a body in an interface. Added in Java 8 to extend interfaces without breaking existing implementations.
```java
interface Greet {
default void hello() {
System.out.println("Hello from interface");
}
}
```
### 22. What is a functional interface?
An interface with exactly one abstract method. Used as the target for lambda expressions. Annotated with `@FunctionalInterface` (optional but recommended).
Examples from `java.util.function`: `Predicate<T>`, `Function<T,R>`, `Consumer<T>`, `Supplier<T>`.
---
## Section 6 — Multithreading
### 23. What is the difference between `Thread` class and `Runnable` interface?
Implementing `Runnable` is preferred because:
- Java doesn't support multiple inheritance — extending `Thread` blocks you from extending another class
- `Runnable` separates the task from the thread execution mechanism
- Better with thread pools (`ExecutorService` accepts `Runnable`)
### 24. What is `synchronized` in Java?
A keyword that ensures only one thread can execute a synchronized method or block at a time, using the object's intrinsic lock (monitor).
```java
synchronized void increment() {
count++;
}
```
### 25. What is a deadlock?
A situation where two or more threads are blocked forever, each waiting for the other to release a lock.
```
Thread A holds Lock 1, waiting for Lock 2
Thread B holds Lock 2, waiting for Lock 1
→ Deadlock
```
Prevention: always acquire locks in a consistent order across all threads.
---
## Section 7 — Object Class Methods
### 26. What does `hashCode()` contract say?
- If `a.equals(b)` is `true`, then `a.hashCode() == b.hashCode()` must be `true`
- If `a.equals(b)` is `false`, `hashCode()` values may or may not be equal
- Override both `equals()` and `hashCode()` together — IDEs and Lombok can generate them
### 27. What is the difference between `toString()` override and no override?
Without override: `ClassName@hashCodeHex` (useless for debugging)
With override: human-readable representation — standard practice for entity and DTO classes.
### 28. What is `clone()` and what interface do you need?
`clone()` creates a copy of the object. The class must implement `Cloneable` (marker interface) or `CloneNotSupportedException` is thrown. Prefer copy constructors over `clone()` in modern Java.
---
## Section 8 — Advanced Topics
### 29. What is the difference between `==` for primitives and autoboxing?
```java
Integer a = 127;
Integer b = 127;
a == b // true — cached range (-128 to 127)
Integer x = 200;
Integer y = 200;
x == y // false — outside cache range, different objects
```
Always use `.equals()` when comparing wrapper types.
### 30. What is garbage collection in Java?
The JVM automatically reclaims memory from objects that are no longer reachable. The `G1` garbage collector is the default since Java 9. You cannot force GC (`System.gc()` is a suggestion, not a command).
### 31. What is the difference between `Stack` and `Heap` in Java?
- **Stack** — stores primitive variables and references to objects, method call frames, thread-local, last-in-first-out
- **Heap** — stores all objects and class instances, shared across threads, garbage collected
### 32. What are access modifiers in Java?
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
| `private` | ✓ | ✗ | ✗ | ✗ |
| `default` | ✓ | ✓ | ✗ | ✗ |
| `protected` | ✓ | ✓ | ✓ | ✗ |
| `public` | ✓ | ✓ | ✓ | ✓ |
---
## Common Mistakes in Java Interviews
1. **Saying `==` compares value for Strings** — it compares references
2. **Confusing `throw` and `throws`**
3. **Not knowing when to use StringBuilder vs String**
4. **Saying ArrayList is "faster" without mentioning the operation type**
5. **Not being able to write a lambda or stream on a whiteboard**
6. **Not knowing that HashMap allows one null key**
7. **Forgetting to override `hashCode()` when overriding `equals()`**
---
## How to Prepare for a Java Interview
1. Understand concepts, not just definitions — draw diagrams if it helps
2. Write code examples for each concept (not just recite them)
3. Practice coding on a whiteboard or plain text editor — no IDE autocomplete
4. Do 5–10 mock interviews before the real one
5. Review your projects — interviewers will ask "how did you implement X in your project?"
---
CodeBegun's Java Full Stack program includes 10 full mock interview sessions before placement. [Explore the program](/java-full-stack) or [WhatsApp us](https://wa.me/916301099587) for the next batch.
Siva Prasad Galaba
Staff Engineer & Founder, CodeBegun
Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaching the next generation to code the way the industry actually works.
