🔹 1. What is Java?
Answer:
Java is a high-level, object-oriented programming language developed by Sun
Microsystems (now Oracle).
It’s platform-independent (thanks to the JVM), secure, and used to build applications from
mobile to enterprise systems.
🔹 2. What is JVM (Java Virtual Machine)?
Answer:
JVM is a part of the Java Runtime Environment (JRE). It executes Java bytecode and
provides platform independence by converting bytecode into machine-specific code.
🔹 3. What is JRE (Java Runtime Environment)?
Answer:
JRE is a software package that contains the JVM and libraries required to run Java
applications, but not compile them.
🔹 4. What is JDK (Java Development Kit)?
Answer:
JDK is the full development kit that includes JRE, compiler (javac), debugger, and other
tools needed to write, compile, and run Java programs.
🔹 5. What is Bytecode?
Answer:
Bytecode is the intermediate code generated by the Java compiler (.class file), which the
JVM can interpret or compile to native machine code.
🔹 6. What is the difference between == and .equals()?
Answer:
• == checks reference equality (memory address).
• .equals() checks value/content equality (overridden in most classes like
String).
🔹 7. What is an Object in Java?
Answer:
An object is an instance of a class. It holds state (fields/attributes) and behavior
(methods/functions).
🔹 8. What is a Class in Java?
Answer:
A class is a blueprint or template for creating objects. It defines fields and methods.
🔹 9. What is Inheritance?
Answer:
Inheritance is a mechanism where a child class acquires properties and behaviors of a
parent class using extends keyword.
🔹 10. What is Polymorphism?
Answer:
Polymorphism means "many forms". It allows a method to behave differently based on the
object that calls it.
Types:
• Compile-time (Method Overloading)
• Runtime (Method Overriding)
🔹 11. What is Encapsulation?
Answer:
Encapsulation is wrapping of data and methods into a single unit (class) and restricting
direct access using access modifiers (like private).
🔹 12. What is Abstraction?
Answer:
Abstraction hides complex implementation details and shows only necessary features via
abstract classes and interfaces.
🔹 13. What is Interface in Java?
Answer:
An interface is a reference type in Java, similar to a class, but with only method
declarations (till Java 7).
From Java 8 onward, it can have default and static methods.
🔹 14. What is Constructor?
Answer:
A constructor is a special method that gets called when an object is created. It has the
same name as the class and no return type.
🔹 15. What is Overloading vs Overriding?
Answer:
• Overloading: Same method name, different parameters (in same class) → Compile-
time
• Overriding: Subclass provides a specific implementation of a superclass method →
Runtime
🔹 16. What is final in Java?
Answer:
• final variable → constant
• final method → cannot be overridden
• final class → cannot be extended
🔹 17. What is static keyword?
Answer:
static defines a member that belongs to the class, not instances. You can access static
fields/methods without creating an object.
🔹 18. What is this keyword?
Answer:
this refers to the current object of the class. Useful to avoid confusion between class
fields and parameters.
🔹 19. What is super keyword?
Answer:
super refers to the parent class and is used to call the parent’s constructor or methods.
🔹 20. What is Exception in Java?
Answer:
An exception is an event that disrupts the normal flow of the program.
Types:
• Checked (e.g., IOException)
• Unchecked (e.g., NullPointerException)
🔹 21. What is try-catch?
Answer:
A block to handle exceptions:
java
CopyEdit
try {
// code that might throw exception
} catch(Exception e) {
// handle exception
}
🔹 22. What is Collection Framework?
Answer:
It’s a set of interfaces and classes to handle groups of objects (like List, Set, Map, etc.).
🔹 23. What is Array vs ArrayList?
Answer:
• Array is fixed size and stores same data type.
• ArrayList is resizable, part of java.util, and stores objects.
🔹 24. What is Garbage Collection?
Answer:
Automatic memory management in Java where unused objects are deleted to free up
memory.
🔹 25. What is Multithreading?
Answer:
Multithreading is the ability of a CPU to execute multiple threads concurrently. Java
supports it using the Thread class and Runnable interface.
🔹 26. What is Synchronization?
Answer:
It’s a mechanism to control access of multiple threads to shared resources, preventing
race conditions.
🔹 27. What is String, StringBuilder, and StringBuffer?
Answer:
• String: Immutable
• StringBuilder: Mutable, not thread-safe
• StringBuffer: Mutable, thread-safe
🔹 28. What is Package in Java?
Answer:
A package is a namespace that organizes related classes and interfaces.
e.g., java.util, java.io
🔹 29. What is the difference between public, private, protected, and
default access?
Same Same Subclas Other
Modifier
Class Package s Packages
public
protect
ed
default
private
🔹 30. What is Lambda Expression (Java 8)?
Answer:
A lambda is a short block of code which takes in parameters and returns a value.
Used in functional programming and with streams.
java
CopyEdit
(x, y) -> x + y
✅ What is a Thread? (Simple Definition)
A thread is the smallest unit of execution within a process.
It’s like a lightweight subprocess that performs tasks within a
larger program.
Multithreading in Java
Definition: Multithreading means executing multiple threads (smaller
units of a process) concurrently to achieve parallelism.
✅ How Java supports multithreading:
• Java has a built-in Thread class and Runnable interface.
• You can create threads by:
o Extending the Thread class
o Implementing the Runnable interface
o Using Executor Framework for thread pooling
o Using Callable + Future for tasks that return results
2. Threads vs Processes
Feature Thread Process
Smallest unit of execution in a
Definition Independent executing program
process
Shares memory with other
Memory Has its own memory space
threads
Communica Complex, via Inter-Process Communication
Easy, via shared variables
tion (IPC)
Overhead Lightweight Heavyweight
One thread crash may affect
Failure One process crash won’t affect others
others
Deadlock
❌ What is it?
Deadlock is a situation where two or more threads are waiting for each
other to release locks, but none ever do – causing the program to
freeze.
🔁 How it happens:
1. Thread A holds Lock 1 and waits for Lock 2.
2. Thread B holds Lock 2 and waits for Lock 1.
3. Neither can proceed – stuck forever.
✅ How to prevent it:
• Lock ordering: Always acquire locks in a fixed order.
• Try-Lock: Use ReentrantLock.tryLock() to avoid blocking.
• Timeout-based locking: Give up if not acquired within time.
• Minimize synchronized blocks.
🔔 5. notify() vs notifyAll()
Used with wait-notify mechanism (via Object class) to manage thread
communication.
Method Description
Wakes up one waiting
notify()
thread
notifyAll Wakes up all waiting
() threads
🧠 Example:
java
CopyEdit
synchronized(obj) {
obj.wait(); // wait until notified
obj.notify(); // wake up one thread
obj.notifyAll(); // wake up all waiting threads
}
⚙️ 6. Runnable vs Callable
Feature Runnable Callable
Yes (T call() throws
Return value? No (void run())
Exception)
Can't throw checked
Exceptions? Can throw checked exceptions
exceptions
Result
Can't get result directly Can get result using Future
handling
Use case Fire-and-forget tasks Tasks that return a result
✅ Example:
java
CopyEdit
Callable<Integer> task = () -> {
return 42;
};
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> future = executor.submit(task);
⚡ 7. Volatile Keyword
🔍 What it does:
• Ensures that the value of a variable is always read from main
memory, not from a thread's cache.
• Prevents thread-local caching of variables.
🚫 What it doesn’t do:
• It doesn't guarantee atomicity.
• It only ensures visibility, not synchronization.
✅ Example:
java
CopyEdit
volatile boolean flag = false;
public void run() {
while (!flag) {
// do something
}
}
Without volatile, one thread may never see the updated value of flag
from another thread.
🧭 Summary Table
Concept Key Point
Multiple threads in one process for
Multithreading
efficiency
Thread vs Process Threads share memory; processes don’t
Synchronization Prevents race conditions
Deadlock Circular lock waiting — avoid by ordering
notify() / notifyAll() Used to wake waiting threads
Runnable vs Runnable: no return; Callable: returns
Callable result
Volatile Ensures variable visibility across threads