1) What is Java Garbage Collection, and how does it work?
Garbage Collection (GC) is an automatic memory management process in Java. It removes
objects from heap memory that are no longer referenced by the program.
How it works:
Objects are created in heap memory
GC identifies unused objects
Frees memory automatically
Common GC techniques:
Mark and Sweep
Generational GC (Young, Old, Metaspace)
👉 Developer doesn’t need to delete objects manually.
2) Difference between == and .equals() in Java
== .equals()
Compares memory references Compares content/value
Used for primitives & objects Used for objects
Cannot be overridden Can be overridden
Example:
String a = new String("Java");
String b = new String("Java");
a == b // false
[Link](b) // true
3) How does Java handle multithreading?
Java supports multithreading using:
Thread class
Runnable interface
Executor framework
Each thread runs independently but shares the same memory.
Key concepts:
Synchronization
Thread lifecycle
Inter-thread communication
Benefits:
Better CPU utilization
Faster execution
Responsive applications
4) Difference between ArrayList and LinkedList
ArrayList LinkedList
Uses dynamic array Uses doubly linked list
Fast access (O(1)) Slow access (O(n))
Slow insertion/deletion Fast insertion/deletion
Less memory overhead More memory (pointers)
5) Difference between HashMap and Hashtable
HashMap Hashtable
Not synchronized Synchronized
Allows one null key & multiple null values No null key or value
Faster Slower
Introduced in Java 1.2 Legacy class
👉 For thread safety, use ConcurrentHashMap.
6) Explain Encapsulation in Java
Encapsulation means binding data and methods together and hiding internal details.
How it’s achieved:
Private variables
Public getters & setters
Example:
class Employee {
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
[Link] = salary;
}
}
Benefits:
Data security
Better control
Code maintainability
7) How do you handle Exceptions in Java?
Java uses exception handling to handle runtime errors gracefully.
Keywords:
try
catch
finally
throw
throws
Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Error occurred");
} finally {
[Link]("Always executed");
}
8) What is JDBC?
JDBC (Java Database Connectivity) is an API that allows Java applications to connect and
interact with databases.
Steps in JDBC:
1. Load driver
2. Create connection
3. Create statement
4. Execute query
5. Close connection
Used for CRUD operations on databases.
9) What is Deadlock?
Deadlock occurs when two or more threads wait indefinitely for resources held by each
other.
Conditions for deadlock:
Mutual exclusion
Hold and wait
No preemption
Circular wait
👉 Result: Program hangs.
10) Difference between Thread and Runnable
Thread Runnable
A class An interface
Extends Thread Implements Runnable
Cannot extend other class Can extend another class
Less flexible More flexible
Runnable is preferred in real-world applications.