🎯 Free Interview Preparation

Java Interview Questions & Answers

Comprehensive Java interview preparation guide.

Java Interview Questions & Answers (2026)

Comprehensive Java interview preparation guide.

Question #1: What are the four pillars of OOP?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Expected Answer

Java follows four OOP principles:

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

Question #2: Difference Between JDK, JRE and JVM

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Comparison

Component Description
JDK Development Kit
JRE Runtime Environment
JVM Java Virtual Machine

Question #3: What is Method Overloading?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Expected Answer

Same method name with different parameter lists.

Code Example

class Calculator {

  int add(int a, int b) {
    return a + b;
  }

  int add(int a, int b, int c) {
    return a + b + c;
  }

}

Question #4: What is Method Overriding?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

class Animal {

  void sound() {
    System.out.println("Animal");
  }

}

class Dog extends Animal {

  @Override
  void sound() {
    System.out.println("Bark");
  }

}

Question #5: Difference Between == and equals()

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

String a = new String("Java");
String b = new String("Java");

System.out.println(a == b);
System.out.println(a.equals(b));

Question #6: What is Constructor?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

class Employee {

  Employee() {
    System.out.println("Created");
  }

}

Question #7: What is Inheritance?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

class Vehicle {

  void start() {}

}

class Car extends Vehicle {

}

Question #8: What is Encapsulation?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

class Employee {

  private String name;

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

}

Question #9: What is Abstraction?

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

abstract class Shape {

  abstract void draw();

}

Question #10: Abstract Class vs Interface

Level: Junior | Category: OOP & Fundamentals | Time: 7 mins

Code Example

interface Payment {

  void pay();

}

class CreditCardPayment implements Payment {

  public void pay() {
    System.out.println("Paid");
  }

}

Mid-Level Question #11: Difference Between ArrayList and LinkedList

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Expected Answer

ArrayList LinkedList
Dynamic Array Doubly Linked List
Fast Random Access Slow Random Access
Slow Insert/Delete in Middle Fast Insert/Delete
Less Memory More Memory
Interviewer Note: 💡 When would you choose LinkedList over ArrayList?

Mid-Level Question #12: How Does HashMap Work Internally?

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Internal Steps

HashMap stores data using key-value pairs.

  1. Calculate hashCode()
  2. Find bucket index
  3. Store entry in bucket
  4. Handle collisions using LinkedList or Tree

Map map = new HashMap<>();

map.put(1, "Java");

map.put(2, "Spring");
Interviewer Note: 💡 Senior What changed in Java 8 when collisions occur?

Mid-Level Question #13: Difference Between HashMap and Hashtable

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Expected Answer

HashMap Hashtable
Not Thread Safe Thread Safe
Allows Null Keys No Null Keys
Faster Slower
Interviewer Note: 💡 Modern applications usually prefer ConcurrentHashMap.

Mid-Level Question #14: Difference Between HashSet and TreeSet

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Expected Answer

HashSet TreeSet
Unordered Sorted
O(1) O(log n)
Uses HashMap Uses TreeMap
Set<Integer> numbers = new TreeSet<>();

numbers.add(10);

numbers.add(5);

numbers.add(20);

Mid-Level Question #15: Comparable vs Comparator

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Expected Answer

Comparable Comparator
Natural Sorting Custom Sorting
compareTo() compare()
Inside Class Separate Class
employees.sort(

  Comparator.comparing(Employee::getSalary)

);

Mid-Level Question #16: Difference Between String, StringBuilder and StringBuffer

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Expected Answer

Type Mutable Thread Safe
String No Yes
StringBuilder Yes No
StringBuffer Yes Yes

StringBuilder sb = new StringBuilder();

sb.append("Java");

sb.append(" Interview");

Mid-Level Question #17: What is a Lambda Expression?

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Expected Answer

Lambda expressions provide a concise way to represent anonymous functions.


List names = List.of("Java", "Spring");

names.forEach(

  name -> System.out.println(name)

);
Interviewer Note: 💡 Introduced in Java 8.

Mid-Level Question #18: What is a Functional Interface?

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Common Functional Interfaces

An interface with exactly one abstract method.

@FunctionalInterface

interface Calculator {

  int add(int a, int b);

}
  • Predicate
  • Function
  • Consumer
  • Supplier

Mid-Level Question #19: What is Stream API?

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Benefits

Stream API enables functional-style processing of collections.

List<Integer> numbers = List.of(1,2,3,4,5);

numbers.stream()

  .filter(n -> n % 2 == 0)

  .forEach(System.out::println);
  • Readable Code
  • Functional Programming
  • Parallel Processing

Mid-Level Question #20: What is Optional in Java 8?

Level: Mid-Level | Category: Collections & Streams | Time: 10 mins

Common Methods

Optional helps avoid NullPointerException.


Optional name =

Optional.ofNullable(null);

System.out.println(

  name.orElse("Guest")

);
  • of()
  • ofNullable()
  • orElse()
  • orElseGet()
  • isPresent()
Interviewer Note: 💡 Why is Optional not recommended as an Entity field?

Senior Question #21: Explain JVM Memory Structure

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Expected Answer

JVM memory is divided into several areas used during program execution.

Memory Area Purpose
Heap Stores Objects
Stack Stores Method Calls & Local Variables
Metaspace Stores Class Metadata
PC Register Current Instruction
Native Stack Native Method Calls
Interviewer Note: 💡 Very common senior Java interview question.

Senior Question #22: Difference Between Heap and Stack Memory

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Expected Answer

Heap Stack
Stores Objects Stores Local Variables
Shared Across Threads Thread Specific
Managed by GC Automatically Cleared
Employee employee = new Employee();

int age = 30;

employee object is stored in Heap while age is stored in Stack.

Senior Question #23: What is Garbage Collection?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Benefits

Garbage Collection automatically removes objects that are no longer reachable.

  • Automatic Memory Management
  • Reduces Memory Leaks
  • Improves Stability
Interviewer Note: 💡 Can we force Garbage Collection? System.gc() is only a request, not a guarantee.

Senior Question #24: What are Different Garbage Collectors?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Expected Answer

GC Usage
Serial GC Small Applications
Parallel GC High Throughput
G1 GC Default Modern GC
ZGC Low Latency Systems
Shenandoah Large Heap Applications

Senior Question #25: What is ClassLoader?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Types

ClassLoader dynamically loads Java classes into memory.

  • Bootstrap ClassLoader
  • Platform ClassLoader
  • Application ClassLoader
Interviewer Note: 💡 What is Parent Delegation Model?

Senior Question #26: What is Serialization?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Uses

Serialization converts an object into a byte stream.

public class Employee

implements Serializable {

  private String name;

}
  • Caching
  • Messaging Systems
  • File Storage
  • Network Transfer

Senior Question #27: What is Reflection API?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Common Framework Usage

Reflection allows inspection and modification of classes at runtime.

Class<?> clazz = Employee.class;

Method[] methods = clazz.getMethods();
  • Spring Framework
  • Hibernate
  • JUnit

Senior Question #28: Difference Between Thread and Process

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Expected Answer

Process Thread
Independent Execution Unit Lightweight Unit
Own Memory Shared Memory
Expensive Cheaper

Senior Question #29: What is Synchronization?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Expected Answer

Synchronization prevents multiple threads from accessing critical sections simultaneously.

public synchronized void deposit() {

  balance += 100;

}
Interviewer Note: 💡 What problems occur without synchronization? Race Conditions and Data Inconsistency.

Senior Question #30: What is ConcurrentHashMap?

Level: Senior | Category: Memory & Concurrency | Time: 12 mins

Advantages

ConcurrentHashMap provides thread-safe access without locking the entire map.


Map users =

new ConcurrentHashMap<>();

users.put(1, " Vijay"); 
  • Thread Safe
  • Better Performance Than Hashtable
  • High Concurrency Support
Interviewer Note: 💡 Very common Spring Boot and Microservices interview question.

Practical Question #31: Reverse a String

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

Write a program to reverse a string without using built-in reverse methods.

String input = "Java";

String reversed = new StringBuilder(input)
  .reverse()
  .toString();

System.out.println(reversed);
Interviewer Note: 💡 Reverse without using extra memory.

Practical Question #32: Check if a String is Palindrome

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

A palindrome reads the same forwards and backwards.

String input = "madam";

String reversed = new StringBuilder(input)
  .reverse()
  .toString();

System.out.println(
  input.equals(reversed)
);
Interviewer Note: 💡 Example: madam, racecar

Practical Question #33: Find Duplicate Characters in a String

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

String str = "java";

Map map = new HashMap<>();

for(char ch : str.toCharArray()) {

  map.put(
    ch,
    map.getOrDefault(ch, 0) + 1
  );

}

System.out.println(map);
Interviewer Note: 💡 Expected output: a → 3 v → 2

Practical Question #34: Find Missing Number in an Array

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

Array contains numbers 1 to N with one missing number.

int[] numbers = {1,2,3,5};

int expected = 5 * (5 + 1) / 2;

int actual = Arrays.stream(numbers).sum();

System.out.println(expected - actual);
Interviewer Note: 💡 Common Amazon, Microsoft and Oracle interview question.

Practical Question #35: Fibonacci Series

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

int a = 0;
int b = 1;

for(int i = 0; i < 10; i++) {

  System.out.print(a + " ");

  int temp = a + b;

  a = b;

  b = temp;

}
Interviewer Note: 💡 Recursive vs Iterative solution.

Practical Question #36: Find Duplicate Elements in an Array

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

int[] numbers = {1,2,3,2,4,1};

Set unique = new HashSet<>();

for(int number : numbers){

  if(!unique.add(number)){

    System.out.println(number);

  }

}
Interviewer Note: 💡 Solve using Stream API.

Practical Question #37: Implement Singleton Pattern

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

Create a thread-safe Singleton.

public class Singleton {

  private static final Singleton INSTANCE =
      new Singleton();

  private Singleton() {}

  public static Singleton getInstance() {

    return INSTANCE;

  }

}
Interviewer Note: 💡 Frequently asked for experienced developers.

Practical Question #38: Create an Immutable Class

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

public final class Employee {

  private final String name;

  public Employee(String name) {

    this.name = name;

  }

  public String getName() {

    return name;

  }

}
Interviewer Note: 💡 Why is String immutable?

Practical Question #39: Sort Employees by Salary Using Stream API

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Expected Answer

employees.stream()

  .sorted(
    Comparator.comparing(
      Employee::getSalary
    )
  )

  .forEach(System.out::println);
Interviewer Note: 💡 Tests Stream API understanding.

Practical Question #40: Design an LRU Cache

Level: Coding Challenge | Category: Algorithms & Patterns | Time: 15 mins

Concepts Tested

Explain how you would implement an LRU Cache in Java.

Map cache =
new LinkedHashMap<>(
  16,
  0.75f,
  true
);
  • HashMap
  • Doubly Linked List
  • Collections Framework
  • Design Skills
Interviewer Note: 💡 Very common product company interview question.

Architect Question #41: How Would You Design a High-Traffic E-Commerce Order System?

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Key Components

  • Order Service
  • Inventory Service
  • Payment Service
  • Notification Service
  • Message Queue
  • Cache Layer

Expected Discussion

  • Database Transactions
  • Event Driven Architecture
  • Scalability
  • Failure Handling

Architect Question #42: How Would You Design a Notification System?

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Channels

  • Email
  • SMS
  • Push Notifications
  • In-App Notifications

Architecture

Client
  ↓
Notification API
  ↓
Message Queue
  ↓
Email / SMS / Push Workers
Interviewer Note: 💡 Discuss retries, queues and failure recovery.

Architect Question #43: Synchronous vs Asynchronous Communication

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Expected Answer

Synchronous Asynchronous
REST API Kafka / RabbitMQ
Immediate Response Event Driven
Tighter Coupling Looser Coupling

Architect Question #44: What is Event Driven Architecture?

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Benefits

Services communicate by publishing and consuming events.

Order Created Event

      ↓

Kafka Topic

      ↓

Inventory Service
Payment Service
Notification Service
  • Loose Coupling
  • Scalability
  • Resilience
  • Independent Deployments

Architect Question #45: Explain Circuit Breaker Pattern

Level: Architect | Category: Architecture & Systems | Time: 15 mins

States

Prevents cascading failures when dependent services are unavailable.

  • Closed
  • Open
  • Half Open
Interviewer Note: 💡 Commonly implemented using Resilience4j.

Architect Question #46: How Would You Handle Distributed Transactions?

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Approaches

  • Two Phase Commit (2PC)
  • Saga Pattern
  • Compensating Transactions
Interviewer Note: 💡 Modern microservices usually prefer Saga Pattern.

Architect Question #47: How Would You Design a Scalable Caching Strategy?

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Cache Layers

  • Application Cache
  • Redis
  • CDN Cache
  • Database Cache

Cache Patterns

  • Cache Aside
  • Write Through
  • Write Behind
  • Read Through

Architect Question #48: How Would You Tune JVM Performance?

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Areas To Investigate

  • Heap Size
  • GC Logs
  • Thread Dumps
  • Memory Leaks
  • CPU Profiling
-Xms2G
-Xmx4G

-XX:+UseG1GC

-XX:MaxGCPauseMillis=200
Interviewer Note: 💡 Strong candidates discuss G1GC and Heap Analysis.

Architect Question #49: Design a URL Shortener Like Bit.ly

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Core Components

  • URL Generator
  • Database
  • Cache
  • Analytics
  • Redirect Service

Scalability Considerations

  • Read Replicas
  • Redis Cache
  • Rate Limiting
  • Load Balancing

Architect Question #50: Design a Real-Time Chat Application

Level: Architect | Category: Architecture & Systems | Time: 15 mins

Core Technologies

  • Spring Boot
  • WebSocket
  • Redis Pub/Sub
  • Kafka
  • MySQL/PostgreSQL
Client

   ↕

WebSocket Server

   ↕

Redis Pub/Sub

   ↕

Chat Service

   ↕

Database

Features

  • Presence Detection
  • Typing Indicators
  • Read Receipts
  • Message Persistence
  • Horizontal Scaling
Interviewer Note: 💡 This question evaluates system design, scalability and distributed systems knowledge.

Frequently Asked Questions

Are Java coding questions included?

Yes. Core Java, Collections, Streams, Multithreading and practical coding questions are covered.

Are Java 8 questions included?

Yes. Stream API, Lambda Expressions, Optional and Functional Interfaces are covered.

Does this cover JVM internals?

Yes. JVM, Heap, Stack, ClassLoader and Garbage Collection are covered.

Is this suitable for senior developers?

Yes. Senior and architect-level topics are included.