***1. Difference between abstract class and interface

Both abstract classes and interfaces are used to achieve abstraction in Java, but they differ in purpose and implementation.


Abstract Class

An abstract class can contain:

  • abstract methods
  • concrete methods

It is used when classes share common behavior.


Example

abstract class Animal {

abstract void sound();

void sleep() {
System.out.println("Sleeping");
}
}

Interface

An interface defines a contract that classes must implement.


Example

interface Animal {

void sound();
}

Difference Table

FeatureAbstract ClassInterface
MethodsAbstract + concreteAbstract methods (default/static allowed from Java 8)
VariablesInstance variables allowedOnly public static final constants
ConstructorsAllowedNot allowed
InheritanceSingle inheritanceMultiple inheritance supported
Keywordextendsimplements

When to Use

SituationPreferred
Shared common behaviorAbstract class
Common contractInterface

Important Interview Point:

Java does not support multiple inheritance using classes, but interfaces provide multiple inheritance capability.


***2. What are marker interfaces?

A marker interface is an interface with no methods or fields.

It is used to provide metadata or special information to JVM/compiler.


Example Marker Interfaces

  • Serializable
  • Cloneable
  • Remote

Example

class Student implements Serializable {
}

Purpose

Marker interfaces tell JVM:

"This class has special behavior."

Common Use Cases

InterfacePurpose
SerializableAllows object serialization
CloneableAllows object cloning

Important Interview Point:

Marker interfaces are also called:

Tag Interfaces

***3. Composition vs Aggregation

Both represent:

HAS-A relationship

but differ in ownership and lifecycle dependency.


Composition

Strong relationship where child object cannot exist independently.


Example

class Engine {
}

class Car {

private Engine engine = new Engine();
}

If Car is destroyed, Engine is also destroyed.


Aggregation

Weak relationship where child object can exist independently.


Example

class Student {
}

class College {

List<Student> students;
}

Students can exist without college.


Difference Table

FeatureCompositionAggregation
RelationshipStrongWeak
DependencyChild depends on parentIndependent
LifecycleSharedSeparate
OwnershipStrong ownershipWeak ownership

Important Interview Point:

Composition represents:

Part-of relationship

***4. Why is composition preferred over inheritance?

Composition is preferred because it provides:

  • loose coupling
  • better flexibility
  • easier maintenance

Problems with Inheritance

  • Tight coupling
  • Fragile hierarchy
  • Difficult maintenance
  • Limited flexibility

Advantages of Composition

1. Better Code Reusability

Behavior can be reused dynamically.


2. Loose Coupling

Changes in one class minimally affect others.


3. Runtime Flexibility

Objects can change behavior dynamically.


Example

class Engine {
void start() {
}
}

class Car {

private Engine engine;

Car(Engine engine) {
this.engine = engine;
}
}

Famous Design Principle

Favor composition over inheritance

Important Interview Point:

Modern design patterns heavily prefer composition.

Examples:

  • Strategy Pattern
  • Decorator Pattern

**5. What is Association in Java?

Association represents a relationship between two separate classes.

It shows how objects communicate with each other.


Example

class Teacher {
}

class Student {

Teacher teacher;
}

Student is associated with Teacher.


Types of Association

1. One-to-One

One object linked with one object.


2. One-to-Many

One object linked with multiple objects.


3. Many-to-Many

Multiple objects linked with multiple objects.


Features

  • Represents relationship
  • Objects remain independent
  • No ownership required

Important Interview Point:

Aggregation and composition are specialized forms of association.


**6. Explain IS-A and HAS-A relationships

These relationships are fundamental in OOP design.


IS-A Relationship

Represents inheritance.


Example

class Animal {
}

class Dog extends Animal {
}

Dog IS-A Animal.


Features

  • Achieved using inheritance
  • Promotes code reuse

HAS-A Relationship

Represents composition or aggregation.


Example

class Engine {
}

class Car {

Engine engine;
}

Car HAS-A Engine.


Difference Table

FeatureIS-AHAS-A
RelationshipInheritanceComposition/Aggregation
KeywordextendsObject reference
CouplingTightLoose

Important Interview Point:

  • IS-A → inheritance
  • HAS-A → composition

**7. What are covariant return types?

Covariant return type means an overridden method can return a subclass type instead of parent type.

Introduced in:

Java 5

Example

class Animal {
}

class Dog extends Animal {
}

class Parent {

Animal getObject() {
return new Animal();
}
}

class Child extends Parent {

Dog getObject() {
return new Dog();
}
}

Benefits

  • More specific return types
  • Improved readability
  • Better type safety

Important Interview Point:

Covariant return types work only in method overriding.


*8. What are virtual methods in Java?

A virtual method is a method whose execution is determined at runtime based on the actual object type.

In Java:

  • all non-static methods are virtual by default

except:

  • private methods
  • static methods
  • final methods

Example

class Animal {

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

class Dog extends Animal {

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

Runtime Behavior

Animal a = new Dog();

a.sound();

Output:

Bark

Method is resolved during runtime.


Why Virtual Methods Matter

They enable:

  • runtime polymorphism
  • dynamic method dispatch

Important Interview Point:

Java achieves runtime polymorphism through virtual methods.

Collections Framework Deep Dive

***9. Internal working of HashMap

HashMap stores data in:

key-value pairs

It uses:

  • hashing
  • bucket arrays
  • linked list / balanced tree

internally.


Basic Working Steps

Step 1: Hash Calculation

When inserting:

map.put(key, value);

HashMap calculates:

hashCode()

of the key.


Step 2: Bucket Index Calculation

Hash is converted into bucket index.

Formula:

index = hash & (n - 1)

where:

  • n = bucket array size

Step 3: Store Entry

Data stored as:

Node<Key, Value>

inside bucket.


Collision Handling

If multiple keys map to same bucket:

  • Java 7 → Linked List
  • Java 8+ → Balanced Tree (after threshold)

Java 8 Optimization

If bucket size exceeds:

8

Linked list converts into:

Red-Black Tree

for faster lookup.


Time Complexity

OperationAverage Complexity
PutO(1)
GetO(1)
RemoveO(1)

Worst case:

O(log n)

in Java 8 tree buckets.


Important Interview Point:

HashMap allows:

  • one null key
  • multiple null values

***10. How does HashSet store elements?

HashSet internally uses:

HashMap

to store elements.


Internal Logic

When adding element:

set.add("Java");

HashSet internally does:

map.put("Java", PRESENT);

Key Points

  • Elements become HashMap keys
  • Dummy object used as value

Internal Dummy Value

private static final Object PRESENT = new Object();

Why Duplicates Are Not Allowed

HashMap keys must be unique.

Therefore HashSet also stores unique elements.


Important Interview Point:

HashSet performance depends on:

  • proper hashCode()
  • proper equals()

implementation.


***11. Difference between HashMap and ConcurrentHashMap

Both store key-value pairs, but they differ in thread safety and concurrency handling.


HashMap

  • Not synchronized
  • Faster
  • Not thread-safe

Example

HashMap<Integer, String> map = new HashMap<>();

ConcurrentHashMap

  • Thread-safe
  • Designed for concurrent access
  • Uses fine-grained locking

Example

ConcurrentHashMap<Integer, String> map =
new ConcurrentHashMap<>();

Difference Table

FeatureHashMapConcurrentHashMap
Thread SafetyNoYes
SynchronizationNonePartial/Fine-grained
PerformanceFaster single-threadedBetter multithreaded
Null Keys/ValuesAllowedNot allowed

Internal Working

HashMap

Single-thread unsafe structure.


ConcurrentHashMap

Java 8 uses:

  • CAS operations
  • bucket-level locking

instead of locking whole map.


Important Interview Point:

ConcurrentHashMap provides better scalability than Hashtable.


***12. Difference between fail-fast and fail-safe iterators

These iterators differ in behavior during concurrent modification.


Fail-Fast Iterator

Throws:

ConcurrentModificationException

if collection is modified during iteration.


Example Collections

  • ArrayList
  • HashMap
  • HashSet

Example

Iterator<Integer> it = list.iterator();

list.add(10);

May throw exception.


Fail-Safe Iterator

Works on cloned copy of collection.

Does not throw exception during modification.


Example Collections

  • ConcurrentHashMap
  • CopyOnWriteArrayList

Difference Table

FeatureFail-FastFail-Safe
Modification DetectionYesNo
ExceptionConcurrentModificationExceptionNo exception
Iteration OverOriginal collectionCopy of collection
PerformanceFasterExtra memory needed

Important Interview Point:

Fail-fast behavior is implemented using:

modCount

internal modification counter.


***13. Why is remove operation faster in LinkedList?

LinkedList removal is faster because elements are connected using nodes.


LinkedList Structure

Each node stores:

  • data
  • previous pointer
  • next pointer

Removal in LinkedList

Only pointer references are updated.

No shifting required.


Example

A ↔ B ↔ C

Removing B:

A ↔ C

ArrayList Removal

Elements must shift after removal.


Complexity

OperationLinkedListArrayList
Remove MiddleO(1)O(n)

Important Interview Point:

LinkedList removal is fast only if node reference is already known.

Searching still takes:

O(n)

**14. How does ArrayList grow dynamically internally?

ArrayList internally uses a dynamic array.

When capacity becomes full:

  • new larger array is created
  • old elements copied

Growth Formula

Java increases capacity by approximately:

50%

Example

If capacity:

10

new capacity becomes:

15

Internal Steps

1. Create Larger Array

newCapacity = oldCapacity + (oldCapacity >> 1)

2. Copy Elements

Uses:

System.arraycopy()

3. Replace Old Array

New array becomes active storage.


Important Interview Point:

Frequent resizing impacts performance.

Use:

ensureCapacity()

for optimization.


**15. Why can’t generic arrays be created?

Java does not allow generic array creation because of:

Type Erasure

Invalid Example

T[] arr = new T[10];

Compile-time error occurs.


Why?

Arrays

Arrays are:

Reified

They know actual type at runtime.


Generics

Generics use:

Type Erasure

Type information removed at runtime.


Problem

Runtime type safety cannot be guaranteed.


Example Risk

Object[] obj = new String[5];

obj[0] = 100;

Causes:

ArrayStoreException

Important Interview Point:

Use collections instead of generic arrays.

Example:

List<T>

**16. Difference between PriorityQueue and TreeSet

Both maintain sorted order but differ in storage and duplicate handling.


PriorityQueue

Maintains priority-based ordering using heap.


Features

  • Allows duplicates
  • Does not maintain full sorted order
  • Head element has highest priority

Example

PriorityQueue<Integer> pq =
new PriorityQueue<>();

TreeSet

Maintains fully sorted unique elements using Red-Black Tree.


Features

  • No duplicates
  • Fully sorted traversal

Example

TreeSet<Integer> set =
new TreeSet<>();

Difference Table

FeaturePriorityQueueTreeSet
Internal StructureHeapRed-Black Tree
DuplicatesAllowedNot allowed
OrderingPartialComplete sorting
Null ValuesNot allowedNot allowed

Important Interview Point:

PriorityQueue guarantees only head priority ordering, not complete sorting.


**17. What is BlockingQueue?

BlockingQueue is a thread-safe queue used in multithreaded environments.

Defined in:

java.util.concurrent

Key Feature

Threads automatically wait when:

  • queue is empty
  • queue is full

Common Methods

MethodBehavior
put()Waits if full
take()Waits if empty

Example

BlockingQueue<Integer> queue =
new ArrayBlockingQueue<>(5);

Common Implementations

  • ArrayBlockingQueue
  • LinkedBlockingQueue
  • PriorityBlockingQueue

Use Cases

  • Producer-Consumer problem
  • Thread pools
  • Task scheduling

Important Interview Point:

BlockingQueue removes need for manual synchronization in producer-consumer scenarios.


*18. Explain EnumSet

EnumSet is a specialized Set implementation for enum types.

Defined in:

java.util

Features

  • High performance
  • Type-safe
  • Internally uses bit vectors

Example

enum Day {
MON, TUE, WED
}

EnumSet<Day> set =
EnumSet.of(Day.MON, Day.WED);

Advantages

  • Faster than HashSet
  • Memory efficient
  • Optimized for enums

Internal Working

Each enum constant represented as a bit.


Important Interview Point:

EnumSet allows only enum values of one enum type.


*19. How does TreeMap work internally?

TreeMap stores key-value pairs in sorted order.

Internally uses:

Red-Black Tree

Features

  • Keys sorted naturally
  • O(log n) operations
  • No null keys

Example

TreeMap<Integer, String> map =
new TreeMap<>();

Internal Working

Step 1: Compare Keys

Keys compared using:

  • Comparable
    or
  • Comparator

Step 2: Insert Node

Node inserted into Red-Black Tree.

Step 3: Tree Balancing

Tree automatically balanced after insertion/deletion.

Why Red-Black Tree?

Provides:

O(log n)

performance consistently.

Time Complexity

OperationComplexity
PutO(log n)
GetO(log n)
RemoveO(log n)

Important Interview Point:

TreeMap sorting is based on keys, not values.

Java Memory & JVM Internals

***20. Explain Java Memory Model

The Java Memory Model (JMM) defines how threads interact with memory in Java.

It specifies:

  • how variables are stored
  • how threads access shared data
  • visibility rules between threads

Purpose of JMM

JMM ensures:

  • thread safety
  • memory consistency
  • predictable multithreading behavior

Main Components of JMM

1. Main Memory

Shared memory area where:

  • objects
  • instance variables
  • static variables

are stored.


2. Thread Local Memory

Each thread has its own working memory.

Thread copies variables from main memory into local cache.


Working Process

Step 1

Thread reads data from main memory.


Step 2

Thread performs operations locally.


Step 3

Updated values written back to main memory.


Important JMM Concepts

ConceptMeaning
VisibilityChanges visible across threads
AtomicityOperations execute completely
OrderingExecution order consistency

Example Problem

Without synchronization:

count++;

may produce inconsistent results in multithreading.


JMM Solutions

  • synchronized
  • volatile
  • locks
  • atomic classes

Important Interview Point:

JMM is mainly designed to solve:

Concurrency and visibility issues

***21. What are Heap, Stack, Metaspace, and Method Area?

These are important JVM memory areas.


1. Heap Memory

Stores:

  • objects
  • instance variables

Shared among all threads.


Example

Student s = new Student();

Object stored in heap.


Features

  • Managed by Garbage Collector
  • Largest memory region

2. Stack Memory

Stores:

  • method calls
  • local variables
  • references

Each thread has its own stack.


Example

int x = 10;

Stored in stack.


Features

  • Faster access
  • Automatically managed

3. Method Area

Stores:

  • class metadata
  • static variables
  • method information
  • constant pool

Shared among threads.


4. Metaspace

Introduced in:

Java 8

Replaced:

Permanent Generation (PermGen)

Features

  • Stores class metadata
  • Uses native memory
  • Dynamically grows

Memory Diagram

JVM Memory
├── Heap
├── Stack
├── Method Area
└── Metaspace

Difference Table

Memory AreaStoresShared
HeapObjectsYes
StackLocal variablesNo
Method AreaClass metadataYes
MetaspaceClass metadata storageYes

Important Interview Point:

Stack memory is thread-specific, while heap memory is shared.


***22. Minor GC vs Major GC vs Full GC

Garbage collection occurs in different memory regions.


JVM Heap Structure

Heap
├── Young Generation
└── Old Generation

1. Minor GC

Occurs in:

Young Generation

Purpose

Removes short-lived objects.


Features

  • Fast
  • Frequent
  • Less pause time

2. Major GC

Occurs in:

Old Generation

Purpose

Removes long-lived unused objects.


Features

  • Slower
  • Less frequent
  • Larger pause time

3. Full GC

Cleans entire heap.

Includes:

  • Young Generation
  • Old Generation
  • Metaspace

Features

  • Slowest GC
  • Causes application pause
  • Expensive operation

Difference Table

GC TypeAreaSpeedFrequency
Minor GCYoung GenFastFrequent
Major GCOld GenSlowLess frequent
Full GCEntire HeapSlowestRare

Important Interview Point:

Frequent Full GC often indicates memory problems or poor JVM tuning.


***23. What is memory leak in Java and causes?

A memory leak occurs when unused objects remain reachable and cannot be garbage collected.


Why Memory Leaks Happen

Garbage Collector removes only unreachable objects.

If references remain:

  • memory cannot be reclaimed

Common Causes

1. Static Collections

static List<Object> list = new ArrayList<>();

Objects remain forever.


2. Unclosed Resources

  • database connections
  • streams
  • sockets

3. Listener Registrations

Objects registered but never removed.


4. Caching Without Limits

Large cache retaining unused objects.


Example

List<Object> list = new ArrayList<>();

while(true) {
list.add(new Object());
}

Effects

  • Increased memory usage
  • Slow performance
  • OutOfMemoryError

Prevention

  • Remove unused references
  • Close resources properly
  • Use WeakHashMap when appropriate

Important Interview Point:

Java has garbage collection, but poor reference management can still cause memory leaks.


**24. What are soft, weak, and phantom references?

Java provides special reference types for advanced memory management.

Defined in:

java.lang.ref

1. Soft Reference

Objects are garbage collected only when memory is low.


Use Case

Caching.


Example

SoftReference<Object> ref =
new SoftReference<>(obj);

2. Weak Reference

Objects are garbage collected as soon as no strong reference exists.


Use Case

WeakHashMap.


Example

WeakReference<Object> ref =
new WeakReference<>(obj);

3. Phantom Reference

Used for post-mortem cleanup tracking.

Object already finalized before reference accessible.


Example

PhantomReference<Object> ref =
new PhantomReference<>(obj, queue);

Difference Table

Reference TypeGC BehaviorUse Case
SoftCollected when memory lowCache
WeakCollected quicklyWeakHashMap
PhantomCleanup trackingResource management

Important Interview Point:

Strong references prevent garbage collection completely.


**25. How does garbage collector work internally?

Garbage Collector automatically removes unused objects from heap memory.


Basic Working Steps

Step 1: Mark Phase

GC identifies reachable objects.


Step 2: Sweep Phase

Unused objects are removed.


Step 3: Compact Phase

Memory fragmentation reduced by rearranging objects.


Reachability Analysis

GC starts from:

GC Roots

Examples:

  • stack references
  • static variables
  • active threads

Generational GC Concept

Most objects:

die young

So JVM divides heap into:

  • Young Generation
  • Old Generation

Young Generation Areas

Eden + Survivor Spaces

Popular Garbage Collectors

GCFeature
Serial GCSingle-threaded
Parallel GCMultiple threads
G1 GCLow pause time
ZGCVery low latency

Important Interview Point:

Modern JVMs use:

Generational Garbage Collection

for performance optimization.


*26. What is OutOfMemoryError?

OutOfMemoryError occurs when JVM cannot allocate enough memory for objects.


Common Causes

1. Memory Leaks

Unused objects retained in memory.


2. Large Object Creation

Creating huge arrays or collections.


3. Infinite Object Creation

while(true) {
list.add(new Object());
}

4. Insufficient Heap Size

JVM heap too small.


Common Types

Error TypeCause
Java heap spaceHeap exhausted
GC overhead limit exceededGC spending too much time
MetaspaceClass metadata overflow
Direct buffer memoryNIO buffer exhaustion

Example

int[] arr = new int[999999999];

May cause:

OutOfMemoryError

Prevention

  • Optimize memory usage
  • Fix memory leaks
  • Tune JVM heap size
  • Use profiling tools

Important Interview Point:

OutOfMemoryError is an Error, not an Exception, and usually indicates serious memory problems.



***27. Why are Strings immutable?

In Java, String objects are immutable, which means once a string object is created, its value cannot be changed.


Example

String s = "Java";

s.concat(" Programming");

System.out.println(s);

Output:

Java

A new object is created instead of modifying the existing one.


Reasons Why Strings are Immutable

1. Security

Strings are heavily used in:

  • database URLs
  • file paths
  • network connections
  • class loading

Immutability prevents accidental or malicious modification.


2. String Pool Optimization

String literals are shared in:

String Constant Pool

Immutability allows multiple references to safely use the same object.


Example

String a = "Java";
String b = "Java";

Both point to same pooled object.


3. Thread Safety

Immutable objects are naturally thread-safe because their state cannot change.

No synchronization required.


4. HashCode Caching

Strings are commonly used as keys in:

  • HashMap
  • Hashtable

Immutability guarantees hashcode consistency.


5. Performance Improvement

String Pool reduces duplicate object creation.


Important Interview Point:

If Strings were mutable, changing one reference could unintentionally affect many other references pointing to the same pooled object.


***28. Benefits of immutable classes

Immutable classes provide many advantages in Java applications.


What is an Immutable Class?

An immutable class is a class whose objects cannot change after creation.

Example:

String

Benefits of Immutable Classes

1. Thread Safety

Immutable objects are automatically thread-safe.

No synchronization needed.


2. Security

Sensitive data cannot be modified after object creation.

Useful for:

  • passwords
  • configuration objects
  • cache keys

3. Simplicity

Objects remain in consistent state.

No unexpected modifications.


4. Safe Sharing

Multiple threads can safely share same object.


5. Better Hashing

Immutable objects are reliable keys for:

HashMap

because hashcode remains constant.


6. Easier Debugging

Object state never changes, making debugging easier.


Real Examples

  • String
  • Integer
  • LocalDate

Important Interview Point:

Immutable classes are widely used in concurrent programming because they eliminate synchronization problems.


**29. How to create immutable class in Java?

An immutable class ensures object state cannot change after creation.


Steps to Create Immutable Class

1. Declare class as final

Prevents inheritance.


2. Make fields private and final

Fields cannot be modified directly.


3. Initialize fields through constructor


4. Do not provide setter methods


5. Return defensive copies for mutable objects


Example

final class Employee {

private final int id;
private final String name;

public Employee(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public String getName() {
return name;
}
}

Features of Above Class

  • Cannot be inherited
  • Fields cannot change
  • No setter methods

Defensive Copy Example

return new Date(date.getTime());

Used for mutable fields like Date.


Important Interview Point:

Making fields final alone does not guarantee immutability if mutable objects are exposed directly.


**30. Difference between StringBuilder and StringBuffer performance

Both StringBuilder and StringBuffer are mutable string classes, but they differ in synchronization and performance.


StringBuffer

  • Thread-safe
  • Methods are synchronized

Example

StringBuffer sb = new StringBuffer();

StringBuilder

  • Not thread-safe
  • No synchronization

Example

StringBuilder sb = new StringBuilder();

Performance Difference

Because StringBuffer uses synchronization:

  • additional locking overhead occurs
  • execution becomes slower

StringBuilder avoids synchronization, so it is faster.


Difference Table

FeatureStringBufferStringBuilder
Thread SafeYesNo
SynchronizationYesNo
PerformanceSlowerFaster
Introduced InJava 1.0Java 1.5

When to Use

SituationRecommended
Multithreaded environmentStringBuffer
Single-threaded environmentStringBuilder

Important Interview Point:

In modern applications, StringBuilder is preferred unless thread safety is required.


*31. Explain String interning

String interning is the process of storing only one copy of identical string literals in the String Pool.


Purpose

  • Save memory
  • Improve performance
  • Reuse string objects

Example

String s1 = "Java";
String s2 = "Java";

Both references point to same pooled object.


Internal Working

When JVM encounters a string literal:

  1. Checks String Pool
  2. If string exists → reuse reference
  3. Otherwise → create new object in pool

Example with new Keyword

String s1 = new String("Java");
String s2 = s1.intern();

intern() returns pooled reference.


String Pool Behavior

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

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

Output:

false

Because:

  • a → String Pool
  • b → Heap object

Using intern()

String c = b.intern();

System.out.println(a == c);

Output:

true

Advantages

  • Reduces duplicate objects
  • Saves heap memory
  • Improves performance

Important Interview Point:

Only string literals are automatically interned. Objects created using new String() are not pooled unless intern() is called.

Exception Handling Advanced

***32. Custom exceptions in Java

Custom exceptions are user-defined exceptions created to represent application-specific errors.

They help provide meaningful error handling.


Why Custom Exceptions Are Needed

Built-in exceptions may not clearly represent business logic errors.

Example:

  • Invalid employee ID
  • Insufficient balance
  • Age restriction violation

How to Create Custom Exception

Create a class extending:

  • Exception → checked exception
    or
  • RuntimeException → unchecked exception

Example

class InvalidAgeException extends Exception {

public InvalidAgeException(String message) {
super(message);
}
}

Using Custom Exception

class Test {

static void validateAge(int age)
throws InvalidAgeException {

if(age < 18) {
throw new InvalidAgeException(
"Not eligible to vote"
);
}
}

public static void main(String[] args) {

try {
validateAge(15);
}
catch(InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
}

Advantages

  • Better readability
  • Business-specific handling
  • Cleaner code design

Important Interview Point:

Use checked exceptions for recoverable conditions and unchecked exceptions for programming errors.


***33. Difference between checked and unchecked exceptions with examples

Java exceptions are categorized into:

  1. Checked Exceptions
  2. Unchecked Exceptions

Checked Exceptions

Checked at:

Compile Time

Compiler forces handling.


Examples

  • IOException
  • SQLException
  • FileNotFoundException

Example

FileReader file =
new FileReader("test.txt");

Compiler requires:

  • try-catch
    or
  • throws

Unchecked Exceptions

Occur at:

Runtime

Compiler does not force handling.


Examples

  • NullPointerException
  • ArithmeticException
  • ArrayIndexOutOfBoundsException

Example

int x = 10 / 0;

Difference Table

FeatureChecked ExceptionUnchecked Exception
Checked ByCompilerJVM
Handling MandatoryYesNo
Occurs AtCompile timeRuntime
Parent ClassExceptionRuntimeException

Hierarchy

Throwable
├── Exception
│ ├── Checked Exceptions
│ └── RuntimeException
│ └── Unchecked Exceptions
└── Error

Important Interview Point:

Unchecked exceptions usually indicate programming mistakes.


***34. Can we have try without catch?

Yes, a try block can exist without catch, but it must be followed by:

finally

Valid Syntax

try {

System.out.println("Try block");

}
finally {

System.out.println("Finally block");
}

Invalid Syntax

try {

}

This causes compile-time error because:

  • either catch
    or
  • finally

is mandatory.


Why Use try-finally?

Used mainly for:

  • resource cleanup
  • file closing
  • database connection release

Important Interview Point:

A try block must always be followed by at least one:

  • catch
    or
  • finally

block.


**35. What happens if return statement exists in try and finally?

If both try and finally contain return statements, the finally block return overrides the try block return.


Example

class Test {

static int test() {

try {
return 10;
}
finally {
return 20;
}
}

public static void main(String[] args) {
System.out.println(test());
}
}

Output

20

Why?

finally block always executes before method completes.


Important Warning

Returning from finally is considered:

Bad Practice

because it may:

  • hide exceptions
  • confuse debugging

Important Interview Point:

finally execution has higher priority than try return.


**36. What is multi-catch block?

A multi-catch block allows handling multiple exception types in a single catch block.

Introduced in:

Java 7

Syntax

catch(ExceptionType1 | ExceptionType2 e)

Example

try {

int[] arr = new int[5];

arr[10] = 50;

}
catch(ArrayIndexOutOfBoundsException |
ArithmeticException e) {

System.out.println("Exception handled");
}

Advantages

  • Reduces duplicate code
  • Cleaner exception handling
  • Better readability

Important Rule

Exceptions in multi-catch must NOT have parent-child relationship.


Invalid Example

catch(Exception | IOException e)

Because:

IOException is subclass of Exception

Important Interview Point:

The exception variable in multi-catch is implicitly final.


**37. What is try-with-resources?

try-with-resources automatically closes resources after usage.

Introduced in:

Java 7

Purpose

Simplifies resource management.


Resources Commonly Used

  • File streams
  • Database connections
  • BufferedReader
  • Scanner

Example

try(BufferedReader br =
new BufferedReader(
new FileReader("test.txt"))) {

System.out.println(br.readLine());

}
catch(IOException e) {

e.printStackTrace();
}

Internal Working

Resources implementing:

AutoCloseable

are automatically closed.


Advantages

  • Cleaner code
  • Prevents resource leaks
  • No need for finally block

Before Java 7

Resources closed manually using:

finally

Important Interview Point:

try-with-resources reduces boilerplate code significantly.


*38. What is suppressed exception?

A suppressed exception occurs when:

  • one exception is thrown in try
    and
  • another exception occurs while closing resources

The second exception becomes:

Suppressed Exception

Example Scenario

  1. Exception occurs in try block
  2. Resource closing also throws exception
  3. Original exception preserved
  4. Closing exception suppressed

Example

try(MyResource r = new MyResource()) {

throw new Exception("Main Exception");

}

If close() also throws exception:

  • JVM suppresses close exception

Accessing Suppressed Exceptions

exception.getSuppressed();

Why Important?

Before Java 7:

  • original exceptions could be lost

Try-with-resources preserves them safely.


Important Interview Point:

Suppressed exceptions are mainly associated with:

try-with-resources
Multithreading & Concurrency

***39. Difference between synchronized method and synchronized block

Both are used for thread synchronization, but they differ in scope and flexibility.


Synchronized Method

Entire method becomes synchronized.


Example

synchronized void display() {

System.out.println("Thread-safe method");
}

Lock Applied On

  • Current object (this) for instance methods
  • Class object for static methods

Synchronized Block

Only a specific block of code is synchronized.


Example

void display() {

synchronized(this) {
System.out.println("Critical section");
}
}

Advantages

  • Better performance
  • Smaller locking scope
  • More flexibility

Difference Table

FeatureSynchronized MethodSynchronized Block
Lock ScopeEntire methodSpecific block
PerformanceSlowerFaster
FlexibilityLessMore
Lock ControlAutomaticCustom object possible

Important Interview Point:

Synchronized blocks are preferred because they reduce lock contention.


***40. What is synchronization and why is it needed?

Synchronization is a mechanism that controls access to shared resources by multiple threads.

It ensures:

Only one thread accesses critical section at a time

Why Synchronization is Needed

Without synchronization:

  • data inconsistency may occur
  • race conditions may happen

Example Without Synchronization

count++;

Multiple threads may update incorrect values simultaneously.


Example With Synchronization

synchronized void increment() {
count++;
}

Benefits

  • Prevents race conditions
  • Maintains data consistency
  • Ensures thread safety

Drawback

  • Performance overhead due to locking

Important Interview Point:

Synchronization mainly protects:

Critical Sections

of code.


***41. Difference between wait(), notify(), and notifyAll()

These methods are used for:

Inter-thread communication

Defined in:

Object class

wait()

Causes current thread to:

  • release lock
  • enter waiting state

Example

obj.wait();

notify()

Wakes up:

One waiting thread

Example

obj.notify();

notifyAll()

Wakes up:

All waiting threads

Example

obj.notifyAll();

Difference Table

MethodPurpose
wait()Releases lock and waits
notify()Wakes one waiting thread
notifyAll()Wakes all waiting threads

Important Rules

  • Must be called inside synchronized block
  • Operate on object monitor

Important Interview Point:

wait() releases lock, while sleep() does not.


***42. What is deadlock?

Deadlock occurs when two or more threads wait indefinitely for each other’s resources.

As a result:

Program execution stops permanently

Example Scenario

Thread 1

Holds Lock A and waits for Lock B.

Thread 2

Holds Lock B and waits for Lock A.

Neither thread proceeds.


Example

Thread 1:
synchronized(lockA) {
synchronized(lockB) {
}
}

Thread 2:
synchronized(lockB) {
synchronized(lockA) {
}
}

Conditions for Deadlock

1. Mutual Exclusion

Only one thread uses resource at a time.


2. Hold and Wait

Thread holds one resource and waits for another.


3. No Preemption

Resources cannot be forcefully taken away.


4. Circular Wait

Threads form circular dependency.


Prevention Techniques

  • Lock ordering
  • Timeout locks
  • Avoid nested synchronization

Important Interview Point:

Deadlocks are difficult to debug in large concurrent applications.


***43. What is race condition?

A race condition occurs when multiple threads access and modify shared data simultaneously, causing unpredictable results.


Example

count++;

This operation is not atomic.


Problem

Two threads may:

  • read same value
  • modify simultaneously
  • overwrite updates

Example Scenario

Initial value:

count = 5

Two threads increment simultaneously.

Expected:

7

Actual:

6

Causes

  • Shared mutable data
  • Lack of synchronization

Prevention

  • synchronized
  • locks
  • atomic variables

Important Interview Point:

Race conditions are one of the most common multithreading bugs.


***44. What is volatile keyword?

volatile ensures visibility of variable changes across threads.


Purpose

When one thread updates variable value:

  • other threads immediately see updated value

Example

volatile boolean flag = true;

Without volatile

Threads may cache old value locally.


With volatile

Variable always read from:

Main Memory

Important Features

  • Guarantees visibility
  • Prevents instruction reordering
  • Does NOT provide full synchronization

Limitation

Operations like:

count++;

are still not thread-safe.


Use Cases

  • Status flags
  • Shutdown signals
  • Configuration updates

Important Interview Point:

volatile provides visibility, not atomicity.


**45. What is ThreadLocal?

ThreadLocal provides separate copies of variables for each thread.

Each thread gets:

Its own independent value

Example

ThreadLocal<Integer> local =
new ThreadLocal<>();

Setting Value

local.set(100);

Getting Value

local.get();

Advantages

  • Avoids shared data conflicts
  • No synchronization needed
  • Thread-safe storage

Common Use Cases

  • Database connections
  • User sessions
  • Transaction management

Important Interview Point:

Each thread maintains its own isolated ThreadLocal value.


**46. Callable vs Runnable

Both represent tasks executed by threads.


Runnable

Introduced in:

Java 1.0

Features

  • No return value
  • Cannot throw checked exceptions

Method

run()

Callable

Introduced in:

Java 5

Features

  • Returns value
  • Can throw exceptions

Method

call()

Example

Callable<Integer> task = () -> {
return 100;
};

Difference Table

FeatureRunnableCallable
Return ValueNoYes
Checked ExceptionNoYes
Methodrun()call()
IntroducedJava 1.0Java 5

Important Interview Point:

Callable tasks are executed using:

ExecutorService

and return:

Future

objects.


**47. ExecutorService in Java

ExecutorService is a framework for managing and executing threads efficiently.

Defined in:

java.util.concurrent

Purpose

  • Reuse threads
  • Manage thread lifecycle
  • Improve performance

Example

ExecutorService service =
Executors.newFixedThreadPool(3);

Submitting Task

service.submit(() -> {
System.out.println("Task running");
});

Shutdown

service.shutdown();

Advantages

  • Better resource management
  • Reduces thread creation overhead
  • Supports thread pools

Common Methods

MethodPurpose
submit()Submit task
execute()Execute Runnable
shutdown()Stop executor

Important Interview Point:

ExecutorService is preferred over manual thread creation in enterprise applications.


**48. What is thread pool?

A thread pool is a collection of reusable worker threads managed by JVM.


Why Thread Pool is Needed

Creating threads repeatedly is expensive.

Thread pool:

  • reuses existing threads
  • improves performance
  • reduces memory overhead

Working Process

Step 1

Threads created in advance.


Step 2

Tasks submitted to queue.


Step 3

Available thread executes task.


Example

ExecutorService service =
Executors.newFixedThreadPool(5);

Types of Thread Pools

Pool TypeDescription
FixedThreadPoolFixed number of threads
CachedThreadPoolDynamic threads
SingleThreadExecutorOne thread only
ScheduledThreadPoolScheduled tasks

Advantages

  • Better performance
  • Reduced thread creation cost
  • Controlled concurrency

Important Interview Point:

Thread pools are widely used in:

  • web servers
  • enterprise applications
  • microservices
  • asynchronous processing

*49. What is ReentrantLock?

ReentrantLock is a locking mechanism from:

java.util.concurrent.locks

It provides explicit locking with more flexibility than synchronized.


Why It Is Called Reentrant

The same thread can acquire the same lock multiple times without deadlock.


Example

ReentrantLock lock =
new ReentrantLock();

lock.lock();

try {

System.out.println("Critical section");

}
finally {

lock.unlock();
}

Features

  • Explicit locking
  • Better thread control
  • Fair locking support
  • Interruptible locking
  • Try-lock capability

Important Methods

MethodPurpose
lock()Acquire lock
unlock()Release lock
tryLock()Try acquiring lock
lockInterruptibly()Interruptible locking

Difference from synchronized

FeaturesynchronizedReentrantLock
Lock ControlAutomaticManual
Fairness PolicyNoYes
tryLock() SupportNoYes
InterruptibleNoYes

Important Interview Point:

Always release ReentrantLock inside:

finally

to avoid deadlocks.


Java 8 Features

***50. What are lambda expressions?

Lambda expressions provide a concise way to write anonymous functions.

Introduced in:

Java 8

Purpose

  • Reduce boilerplate code
  • Support functional programming
  • Simplify anonymous classes

Syntax

(parameters) -> expression

Example

Runnable r = () -> {
System.out.println("Running");
};

Before Lambda

Runnable r = new Runnable() {

public void run() {
System.out.println("Running");
}
};

Advantages

  • Cleaner code
  • Improved readability
  • Easier collection processing

Common Use Cases

  • Streams API
  • Event handling
  • Multithreading

Important Interview Point:

Lambda expressions work mainly with:

Functional Interfaces

***51. What is functional interface?

A functional interface is an interface containing exactly:

One abstract method

Used mainly with:

  • lambda expressions
  • method references

Example

@FunctionalInterface
interface Calculator {

int add(int a, int b);
}

Features

  • Can contain default/static methods
  • Only one abstract method allowed

Common Functional Interfaces

InterfacePurpose
RunnableNo input/output
CallableReturns value
PredicateBoolean result
FunctionInput → Output
ConsumerConsumes input

Example Using Lambda

Calculator c = (a, b) -> a + b;

Important Interview Point:

@FunctionalInterface annotation is optional but recommended.


***52. What is Stream API?

Stream API is used for processing collections of data declaratively.

Introduced in:

Java 8

Purpose

  • Simplify collection operations
  • Support functional programming
  • Enable parallel processing

Features

  • Functional style
  • Lazy evaluation
  • Internal iteration

Example

List<Integer> list =
Arrays.asList(1,2,3,4,5);

list.stream()
.filter(x -> x % 2 == 0)
.forEach(System.out::println);

Common Stream Operations

OperationPurpose
filter()Select elements
map()Transform data
sorted()Sorting
collect()Collect results
forEach()Iterate elements

Stream Types

Intermediate Operations

Return stream.

Examples:

  • filter()
  • map()

Terminal Operations

Produce final result.

Examples:

  • collect()
  • count()

Important Interview Point:

Streams do not modify original collection.


***53. Difference between map() and flatMap()

Both are Stream API operations used for transformation.


map()

Transforms each element individually.


Example

List<String> names =
Arrays.asList("java", "python");

names.stream()
.map(String::toUpperCase)
.forEach(System.out::println);

flatMap()

Transforms and flattens nested structures into single stream.


Example

List<List<Integer>> list =
Arrays.asList(
Arrays.asList(1,2),
Arrays.asList(3,4)
);

list.stream()
.flatMap(x -> x.stream())
.forEach(System.out::println);

Difference Table

Featuremap()flatMap()
OutputOne-to-one mappingFlattened mapping
StructurePreserves nestingRemoves nesting
Use CaseSimple transformationNested collections

Important Interview Point:

flatMap() is heavily used in nested collection processing.


***54. What is Optional class?

Optional is a container object introduced in Java 8 to handle:

Null values safely

Purpose

Reduce:

NullPointerException

Example

Optional<String> name =
Optional.of("Java");

Common Methods

MethodPurpose
of()Create non-null Optional
ofNullable()Allow null
isPresent()Check value
get()Retrieve value
orElse()Default value

Example

String result =
Optional.ofNullable(null)
.orElse("Default");

Advantages

  • Cleaner null handling
  • Reduces null checks
  • Improves readability

Important Interview Point:

Avoid excessive use of:

get()

without checking presence.


**55. What are method references?

Method references provide shorthand syntax for lambda expressions that call existing methods.


Syntax

ClassName::methodName

Example

list.forEach(System.out::println);

Equivalent lambda:

list.forEach(x -> System.out.println(x));

Types of Method References

TypeExample
Static MethodMath::abs
Instance Methodobj::display
Constructor ReferenceStudent::new

Advantages

  • Cleaner syntax
  • Better readability
  • Less boilerplate

Important Interview Point:

Method references are valid only when lambda simply calls another method.


**56. Difference between Collection stream() and parallelStream()

Both create streams, but execution differs.


stream()

Processes elements sequentially.


Example

list.stream()

parallelStream()

Processes elements in parallel using multiple threads.


Example

list.parallelStream()

Difference Table

Featurestream()parallelStream()
ExecutionSequentialParallel
Threads UsedSingleMultiple
PerformanceBetter for small tasksBetter for large CPU tasks
OrderMaintainedMay vary

Internal Working

parallelStream() uses:

ForkJoinPool

Important Interview Point:

Parallel streams may reduce performance for small collections because of thread overhead.


**57. What is default method in interface?

Default methods allow interfaces to contain method implementations.

Introduced in:

Java 8

Purpose

Enable adding new methods to interfaces without breaking existing implementations.


Example

interface Vehicle {

default void start() {
System.out.println("Vehicle Started");
}
}

Features

  • Provides implementation inside interface
  • Inheritable by implementing classes

Why Needed

Before Java 8:
adding method to interface would break all implementing classes.


Important Interview Point:

Default methods help achieve backward compatibility.


*58. What is Nashorn engine?

Nashorn is a JavaScript engine introduced in:

Java 8

It allows Java applications to execute JavaScript code.


Package

javax.script

Example

ScriptEngineManager manager =
new ScriptEngineManager();

ScriptEngine engine =
manager.getEngineByName("nashorn");

engine.eval("print('Hello')");

Features

  • Lightweight JavaScript execution
  • JVM integration
  • Better performance than Rhino

Status

Nashorn was:

  • deprecated in Java 11
  • removed in Java 15

Important Interview Point:

Nashorn enabled seamless Java and JavaScript interoperability inside JVM.

Serialization

***59. What is serialization and deserialization?

Serialization is the process of converting an object into a byte stream so it can be:

  • stored in file
  • transferred over network
  • saved in database

Deserialization is the reverse process of converting byte stream back into object.


Why Serialization is Used

  • Object persistence
  • Network communication
  • Distributed systems
  • Caching

Serialization Process

Object → Byte Stream

Deserialization Process

Byte Stream → Object

Example

Serialization

ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("data.ser"));

out.writeObject(obj);

Deserialization

ObjectInputStream in =
new ObjectInputStream(
new FileInputStream("data.ser"));

Student s = (Student) in.readObject();

Important Classes

ClassPurpose
ObjectOutputStreamSerialize object
ObjectInputStreamDeserialize object

Advantages

  • Easy object storage
  • Simplifies data transfer
  • Supports distributed computing

Important Interview Point:

Only objects implementing:

Serializable

can be serialized.

***60. What is Serializable interface?

Serializable is a marker interface used to indicate that an object can be serialized.

Defined in:

java.io

Why It Is Called Marker Interface

Because it contains:

No methods

Example

import java.io.Serializable;

class Student implements Serializable {

int id;
String name;
}

Purpose

JVM checks whether class implements:

Serializable

before serialization.


What Happens If Not Implemented?

JVM throws:

NotSerializableException

Features

  • Enables serialization
  • Supports object persistence
  • Used in distributed systems

Important Interview Point:

All non-transient instance variables are serialized automatically.


**61. How to prevent serialization of fields?

Fields can be prevented from serialization using:

transient

keyword.


Example

class Student implements Serializable {

int id;

transient String password;
}

Serialization Result

  • id → serialized
  • password → skipped

Why Use transient?

Sensitive or temporary data should not be stored.


Common Use Cases

  • Passwords
  • OTPs
  • Security tokens
  • Temporary cache data

After Deserialization

Transient fields get:

Default values

Example:

null, 0, false

Important Interview Point:

static variables are also not serialized because they belong to class, not object.


**62. What is serialVersionUID?

serialVersionUID is a unique identifier used during serialization and deserialization.

It verifies compatibility between:

  • serialized object
  • loaded class

Example

class Student implements Serializable {

private static final long
serialVersionUID = 1L;
}

Why It Is Needed

Suppose:

  1. Object serialized using old class version
  2. Class structure changes later

JVM compares:

serialVersionUID

If mismatch occurs:

InvalidClassException

is thrown.


Benefits

  • Version control for serialized objects
  • Prevents compatibility issues
  • Improves reliability

If Not Declared

JVM generates automatically.

But generated value may change unexpectedly.


Important Interview Point:

Always declare explicit:

serialVersionUID

in serializable classes.


*63. Difference between Externalizable and Serializable

Both are used for object serialization, but they differ in control and implementation.


Serializable

Java automatically handles serialization.


Example

class Student implements Serializable {
}

Features

  • Easy to use
  • Automatic serialization
  • Less control

Externalizable

Provides complete control over serialization process.

Extends:

Serializable

Example

class Student implements Externalizable {

public void writeExternal(
ObjectOutput out)
throws IOException {

out.writeInt(id);
}

public void readExternal(
ObjectInput in)
throws IOException {

id = in.readInt();
}
}

Required Methods

MethodPurpose
writeExternal()Custom serialization
readExternal()Custom deserialization

Difference Table

FeatureSerializableExternalizable
ControlAutomaticManual
PerformanceSlowerFaster
Ease of UseSimpleComplex
Methods RequiredNoYes
Constructor RequirementNoPublic no-arg required

When to Use

SituationRecommended
Simple serializationSerializable
Customized high-performance serializationExternalizable

Important Interview Point:

Externalizable gives full control but increases coding responsibility.

JDBC

***64. What is JDBC?

JDBC stands for:

Java Database Connectivity

It is an API used to connect Java applications with databases.


Purpose of JDBC

JDBC allows Java programs to:

  • connect to database
  • execute SQL queries
  • retrieve data
  • update database records

Supported Databases

  • MySQL
  • Oracle
  • PostgreSQL
  • SQL Server

JDBC Architecture

Java Application

JDBC API

JDBC Driver

Database

Important JDBC Components

ComponentPurpose
DriverManagerManages drivers
ConnectionDatabase connection
StatementExecute SQL
ResultSetStore query result

Example

Connection con =
DriverManager.getConnection(
url, username, password);

Advantages

  • Database-independent API
  • Easy database communication
  • Supports transactions

Important Interview Point:

JDBC is a specification/API, while JDBC drivers are implementations provided by database vendors.


***65. Steps to connect Java application with database

There are standard steps to connect Java with a database using JDBC.


Step 1: Import JDBC Packages

import java.sql.*;

Step 2: Load JDBC Driver

Class.forName(
"com.mysql.cj.jdbc.Driver");

Step 3: Establish Connection

Connection con =
DriverManager.getConnection(
url, user, password);

Step 4: Create Statement

Statement stmt =
con.createStatement();

Step 5: Execute Query

ResultSet rs =
stmt.executeQuery(
"SELECT * FROM student");

Step 6: Process Result

while(rs.next()) {

System.out.println(
rs.getInt(1));
}

Step 7: Close Connection

con.close();

Complete Flow

Load Driver

Create Connection

Create Statement

Execute Query

Process Result

Close Resources

Important Interview Point:

Modern JDBC drivers auto-register themselves, so explicit:

Class.forName()

is often optional.


***66. What is JDBC DriverManager?

DriverManager is a class used to manage JDBC drivers and establish database connections.

Defined in:

java.sql

Main Responsibilities

  • Loads JDBC drivers
  • Establishes database connections
  • Maintains driver list

Example

Connection con =
DriverManager.getConnection(
url, user, password);

Important Methods

MethodPurpose
getConnection()Create DB connection
registerDriver()Register driver
deregisterDriver()Remove driver

Working Process

  1. DriverManager finds suitable JDBC driver
  2. Creates connection with database
  3. Returns Connection object

Important Interview Point:

DriverManager acts as a factory for database connections.


***67. What is ResultSet?

ResultSet is an object used to store data returned from SQL queries.

Defined in:

java.sql

Purpose

Used to:

  • read database records
  • traverse query results

Example

ResultSet rs =
stmt.executeQuery(
"SELECT * FROM student");

Traversing ResultSet

while(rs.next()) {

System.out.println(
rs.getInt("id"));
}

Important Methods

MethodPurpose
next()Move to next row
getInt()Retrieve integer
getString()Retrieve string

Types of ResultSet

TypeDescription
Forward OnlyMove forward only
ScrollableMove both directions
UpdatableUpdate records

Cursor Concept

ResultSet maintains cursor pointing to current row.

Initially cursor is:

Before first row

Important Interview Point:

Calling:

next()

moves cursor and returns:

  • true if row exists
  • false otherwise

**68. Difference between Statement and PreparedStatement

Both are used to execute SQL queries, but they differ in security and performance.

Statement

Used for static SQL queries.

Example

Statement stmt =
con.createStatement();

Query Example

stmt.executeQuery(
"SELECT * FROM student");

PreparedStatement

Used for parameterized queries.

Example

PreparedStatement ps =
con.prepareStatement(
"SELECT * FROM student WHERE id=?");

Setting Parameter

ps.setInt(1, 101);

Difference Table

FeatureStatementPreparedStatement
Query TypeStaticParameterized
PerformanceSlowerFaster
SQL Injection SafeNoYes
CompilationEvery executionPrecompiled

Why PreparedStatement is Better

  • Prevents SQL Injection
  • Improves performance
  • Easier parameter handling

Important Interview Point:

PreparedStatement is preferred in real-world applications because it is more secure and efficient.

**69. What is connection pooling?

Connection pooling is a technique of reusing database connections instead of creating new connections repeatedly.


Why Needed

Creating database connections is expensive because it involves:

  • network communication
  • authentication
  • resource allocation

Working Process

Step 1

Pool creates multiple connections in advance.


Step 2

Application requests connection from pool.


Step 3

After usage, connection returned to pool instead of closing.


Advantages

  • Better performance
  • Reduced connection overhead
  • Improved scalability

Popular Connection Pool Libraries

  • HikariCP
  • Apache DBCP
  • C3P0

Example Flow

Pool → Give Connection
App → Use Connection
Pool ← Return Connection

Important Interview Point:

Modern enterprise applications almost always use connection pooling for performance optimization.


*70. What is batch processing in JDBC?

Batch processing allows execution of multiple SQL statements together as a batch.

Purpose

Improve performance by reducing database round trips.


Example

Statement stmt =
con.createStatement();

stmt.addBatch(
"INSERT INTO student VALUES(1,'A')");

stmt.addBatch(
"INSERT INTO student VALUES(2,'B')");

stmt.executeBatch();

With PreparedStatement

PreparedStatement ps =
con.prepareStatement(
"INSERT INTO student VALUES(?, ?)");

ps.setInt(1, 1);
ps.setString(2, "Java");

ps.addBatch();

Advantages

  • Faster execution
  • Reduced network calls
  • Efficient bulk operations

Use Cases

  • Bulk inserts
  • Bulk updates
  • ETL operations

Important Methods

MethodPurpose
addBatch()Add query to batch
executeBatch()Execute all queries
clearBatch()Remove batch

Important Interview Point:

Batch processing significantly improves performance for large database operations.

Important Missing Intermediate Questions

***71. What is dependency injection?

Dependency Injection (DI) is a design pattern where dependencies are provided to an object externally instead of the object creating them itself.


Purpose

  • Reduce tight coupling
  • Improve testability
  • Increase flexibility

Without Dependency Injection

class Car {

Engine engine = new Engine();
}

Car creates dependency itself.

This causes:

Tight Coupling

With Dependency Injection

class Car {

private Engine engine;

Car(Engine engine) {
this.engine = engine;
}
}

Dependency provided externally.


Types of Dependency Injection

TypeDescription
Constructor InjectionDependency through constructor
Setter InjectionDependency through setter
Field InjectionDependency directly into field

Advantages

  • Loose coupling
  • Easier unit testing
  • Better maintainability

Frameworks Using DI

  • Spring Framework
  • Jakarta EE

Important Interview Point:

Dependency Injection follows:

Inversion of Control (IoC)

principle.


***72. What is Singleton design pattern?

Singleton Pattern ensures:

Only one object

of a class exists throughout application lifecycle.


Purpose

Used when exactly one shared instance is required.


Common Use Cases

  • Logger
  • Database connection manager
  • Configuration manager
  • Cache manager

Key Features

  • Private constructor
  • Static instance
  • Global access point

Example

class Singleton {

private static Singleton instance =
new Singleton();

private Singleton() {
}

public static Singleton getInstance() {
return instance;
}
}

Advantages

  • Controlled object creation
  • Shared resource management
  • Saves memory

Important Interview Point:

Singleton pattern is one of the most commonly asked Java design pattern interview questions.


***73. How to implement Singleton in Java?

There are multiple ways to implement Singleton in Java.


1. Eager Initialization

Object created immediately.


Example

class Singleton {

private static final Singleton instance =
new Singleton();

private Singleton() {
}

public static Singleton getInstance() {
return instance;
}
}

Advantages

  • Simple
  • Thread-safe

Disadvantage

Object created even if unused.


2. Lazy Initialization

Object created only when needed.


Example

class Singleton {

private static Singleton instance;

private Singleton() {
}

public static Singleton getInstance() {

if(instance == null) {
instance = new Singleton();
}

return instance;
}
}

Problem

Not thread-safe.


3. Thread-Safe Singleton

Using synchronized method.


Example

public synchronized static
Singleton getInstance() {
}

4. Bill Pugh Singleton (Recommended)

Uses static inner helper class.


Example

class Singleton {

private Singleton() {
}

private static class Helper {

private static final Singleton INSTANCE =
new Singleton();
}

public static Singleton getInstance() {
return Helper.INSTANCE;
}
}

5. Enum Singleton (Best Approach)


Example

enum Singleton {

INSTANCE;
}

Important Interview Point:

Enum Singleton is safest because it prevents:

  • reflection attacks
  • serialization issues

***74. What are immutable objects?

Immutable objects are objects whose state cannot be changed after creation.


Example

String s = "Java";

Strings are immutable.


Features

  • State never changes
  • Thread-safe
  • Easy to share

How to Create Immutable Object

Rules

  • Make class final
  • Make fields private and final
  • No setter methods
  • Initialize through constructor

Example

final class Employee {

private final int id;

Employee(int id) {
this.id = id;
}

public int getId() {
return id;
}
}

Advantages

  • Thread safety
  • Security
  • Simpler design

Important Interview Point:

Immutable objects are heavily used in concurrent programming.


**75. What are Java design patterns?

Design patterns are reusable solutions to commonly occurring software design problems.


Purpose

  • Improve code structure
  • Promote best practices
  • Increase maintainability

Categories of Design Patterns

CategoryPurpose
CreationalObject creation
StructuralObject composition
BehavioralObject interaction

Common Java Design Patterns

PatternCategory
SingletonCreational
FactoryCreational
BuilderCreational
AdapterStructural
ObserverBehavioral

Advantages

  • Reusable solutions
  • Better architecture
  • Easier maintenance

Important Interview Point:

Design patterns are not code, but proven design solutions.


**76. Explain Factory Design Pattern

Factory Pattern creates objects without exposing object creation logic to client.


Purpose

Object creation delegated to factory class.


Example Without Factory

Car c = new Car();

Client directly creates object.


With Factory Pattern

class VehicleFactory {

static Vehicle getVehicle(String type) {

if(type.equals("car")) {
return new Car();
}

return new Bike();
}
}

Usage

Vehicle v =
VehicleFactory.getVehicle("car");

Advantages

  • Loose coupling
  • Centralized object creation
  • Easy scalability

Real-Life Example

Shape Factory

creates:

  • Circle
  • Rectangle
  • Square

Important Interview Point:

Factory Pattern hides object creation complexity from client.


**77. Explain Builder Pattern

Builder Pattern is used to construct complex objects step by step.


Why Needed

Suppose constructor has many parameters:

new Student(1, "A", 20, "Patna", true)

Hard to read and maintain.


Builder Solution


Example

class Student {

private int id;
private String name;

private Student(Builder b) {
this.id = b.id;
this.name = b.name;
}

static class Builder {

private int id;
private String name;

Builder setId(int id) {
this.id = id;
return this;
}

Builder setName(String name) {
this.name = name;
return this;
}

Student build() {
return new Student(this);
}
}
}

Usage

Student s = new Student.Builder()
.setId(1)
.setName("Java")
.build();

Advantages

  • Readable object creation
  • Flexible construction
  • Avoids telescoping constructors

Important Interview Point:

Builder Pattern is commonly used in:

  • Lombok
  • StringBuilder
  • Java APIs

78. Explain Observer Pattern

Observer Pattern defines:

One-to-many dependency

When one object changes state, all dependent objects are notified automatically.


Components

ComponentRole
SubjectMain object
ObserverDependent objects

Real-Life Example

YouTube Channel Subscription

When creator uploads video:

  • all subscribers notified

Example

interface Observer {

void update();
}

Subject

class YouTubeChannel {

List<Observer> observers =
new ArrayList<>();

void subscribe(Observer o) {
observers.add(o);
}

void notifyUsers() {

for(Observer o : observers) {
o.update();
}
}
}

Advantages

  • Loose coupling
  • Event-driven communication
  • Easy scalability

Common Use Cases

  • Event listeners
  • GUI frameworks
  • Notification systems

Important Interview Point:

Observer Pattern is heavily used in:

  • Java event handling
  • Reactive programming
  • Messaging systems