Question #1: What are the four pillars of OOP?
Expected Answer
Java follows four OOP principles:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Comprehensive Java interview preparation guide.
Comprehensive Java interview preparation guide.
Java follows four OOP principles:
| Component | Description |
|---|---|
| JDK | Development Kit |
| JRE | Runtime Environment |
| JVM | Java Virtual Machine |
Same method name with different parameter lists.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}class Animal {
void sound() {
System.out.println("Animal");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);
System.out.println(a.equals(b));class Employee {
Employee() {
System.out.println("Created");
}
}class Vehicle {
void start() {}
}
class Car extends Vehicle {
}class Employee {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}abstract class Shape {
abstract void draw();
}interface Payment {
void pay();
}
class CreditCardPayment implements Payment {
public void pay() {
System.out.println("Paid");
}
}| 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?
HashMap stores data using key-value pairs.
Map map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring");
Interviewer Note: 💡 Senior What changed in Java 8 when collisions occur?
| HashMap | Hashtable |
|---|---|
| Not Thread Safe | Thread Safe |
| Allows Null Keys | No Null Keys |
| Faster | Slower |
Interviewer Note: 💡 Modern applications usually prefer ConcurrentHashMap.
| 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);| Comparable | Comparator |
|---|---|
| Natural Sorting | Custom Sorting |
| compareTo() | compare() |
| Inside Class | Separate Class |
employees.sort(
Comparator.comparing(Employee::getSalary)
);| Type | Mutable | Thread Safe |
|---|---|---|
| String | No | Yes |
| StringBuilder | Yes | No |
| StringBuffer | Yes | Yes |
StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.append(" Interview");
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.
An interface with exactly one abstract method.
@FunctionalInterface
interface Calculator {
int add(int a, int b);
}
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);
Optional helps avoid NullPointerException.
Optional name =
Optional.ofNullable(null);
System.out.println(
name.orElse("Guest")
);
Interviewer Note: 💡 Why is Optional not recommended as an Entity field?
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.
| 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.
Garbage Collection automatically removes objects that are no longer reachable.
Interviewer Note: 💡 Can we force Garbage Collection? System.gc() is only a request, not a guarantee.
| GC | Usage |
|---|---|
| Serial GC | Small Applications |
| Parallel GC | High Throughput |
| G1 GC | Default Modern GC |
| ZGC | Low Latency Systems |
| Shenandoah | Large Heap Applications |
ClassLoader dynamically loads Java classes into memory.
Interviewer Note: 💡 What is Parent Delegation Model?
Serialization converts an object into a byte stream.
public class Employee
implements Serializable {
private String name;
}
Reflection allows inspection and modification of classes at runtime.
Class<?> clazz = Employee.class;
Method[] methods = clazz.getMethods();
| Process | Thread |
|---|---|
| Independent Execution Unit | Lightweight Unit |
| Own Memory | Shared Memory |
| Expensive | Cheaper |
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.
ConcurrentHashMap provides thread-safe access without locking the entire map.
Map users =
new ConcurrentHashMap<>();
users.put(1, " Vijay");
Interviewer Note: 💡 Very common Spring Boot and Microservices interview question.
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.
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
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
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.
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.
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.
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.
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?
employees.stream()
.sorted(
Comparator.comparing(
Employee::getSalary
)
)
.forEach(System.out::println);Interviewer Note: 💡 Tests Stream API understanding.
Explain how you would implement an LRU Cache in Java.
Map cache =
new LinkedHashMap<>(
16,
0.75f,
true
);
Interviewer Note: 💡 Very common product company interview question.
Client
↓
Notification API
↓
Message Queue
↓
Email / SMS / Push WorkersInterviewer Note: 💡 Discuss retries, queues and failure recovery.
| Synchronous | Asynchronous |
|---|---|
| REST API | Kafka / RabbitMQ |
| Immediate Response | Event Driven |
| Tighter Coupling | Looser Coupling |
Services communicate by publishing and consuming events.
Order Created Event
↓
Kafka Topic
↓
Inventory Service
Payment Service
Notification Service
Prevents cascading failures when dependent services are unavailable.
Interviewer Note: 💡 Commonly implemented using Resilience4j.
Interviewer Note: 💡 Modern microservices usually prefer Saga Pattern.
-Xms2G
-Xmx4G
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200Interviewer Note: 💡 Strong candidates discuss G1GC and Heap Analysis.
Client
↕
WebSocket Server
↕
Redis Pub/Sub
↕
Chat Service
↕
Database
Interviewer Note: 💡 This question evaluates system design, scalability and distributed systems knowledge.
Yes. Core Java, Collections, Streams, Multithreading and practical coding questions are covered.
Yes. Stream API, Lambda Expressions, Optional and Functional Interfaces are covered.
Yes. JVM, Heap, Stack, ClassLoader and Garbage Collection are covered.
Yes. Senior and architect-level topics are included.