Top 50 Java Interview Questions and Answers
1️⃣ What is Java?
Java is a high-level, object-oriented, platform-independent programming language used to build web, mobile, desktop, and enterprise applications.
2️⃣ Why is Java platform independent?
Java code is compiled into bytecode, which runs on the JVM (Java Virtual Machine) instead of directly on the OS.
3️⃣ What is JVM?
JVM is the runtime engine that:
-
Loads bytecode
-
Manages memory
-
Executes Java programs
4️⃣ Difference between JDK, JRE, and JVM?
| Component | Purpose |
|---|---|
| JVM | Executes bytecode |
| JRE | JVM + libraries |
| JDK | JRE + development tools |
5️⃣ What are the main features of Java?
-
Object-Oriented
-
Platform Independent
-
Secure
-
Robust
-
Multithreaded
-
Automatic Garbage Collection
6️⃣ What is Object-Oriented Programming?
OOP is a programming model based on objects that contain data and methods.
7️⃣ What are the four pillars of OOP?
-
Encapsulation
-
Inheritance
-
Polymorphism
-
Abstraction
8️⃣ What is Encapsulation?
Wrapping data and methods together and hiding internal details using private variables and public methods.
9️⃣ What is Inheritance?
One class acquiring properties of another class using extends.
🔟 What is Polymorphism?
Ability of an object to take many forms.
-
Compile-time → Method Overloading
-
Runtime → Method Overriding
1️⃣1️⃣ What is Abstraction?
Hiding implementation details and showing only essential features using:
-
Abstract classes
-
Interfaces
1️⃣2️⃣ Difference between Abstract Class and Interface?
| Abstract Class | Interface |
|---|---|
| Can have constructors | No constructors |
| Can have variables | Constants only |
| Supports inheritance | Supports multiple inheritance |
1️⃣3️⃣ What is an Interface?
An interface defines a contract. Classes implementing it must provide method implementations.
1️⃣4️⃣ What is Method Overloading?
Same method name with different parameters in the same class.
1️⃣5️⃣ What is Method Overriding?
Subclass provides a specific implementation of a parent class method.
1️⃣6️⃣ What is the final keyword?
-
final variable→ constant -
final method→ cannot override -
final class→ cannot extend
1️⃣7️⃣ What is a Constructor?
A special method used to initialize objects.
Name is same as class name.
1️⃣8️⃣ Can constructors be overridden?
❌ No. Constructors cannot be inherited or overridden.
1️⃣9️⃣ What is this keyword?
Refers to the current object of the class.
2️⃣0️⃣ What is static keyword?
Belongs to the class, not the object.
2️⃣1️⃣ What is Garbage Collection?
Automatic memory cleanup of unused objects by JVM.
2️⃣2️⃣ Difference between == and equals()?
| == | equals() |
|---|---|
| Compares reference | Compares content |
2️⃣3️⃣ What is String immutability?
Once created, a String object cannot be changed.
2️⃣4️⃣ Difference between String, StringBuilder, StringBuffer?
| Type | Mutable | Thread-safe |
|---|---|---|
| String | ❌ No | ✅ Yes |
| StringBuilder | ✅ Yes | ❌ No |
| StringBuffer | ✅ Yes | ✅ Yes |
2️⃣5️⃣ What is Exception?
An unexpected runtime event that disrupts program flow.
2️⃣6️⃣ Difference between Checked and Unchecked Exceptions?
| Checked | Unchecked |
|---|---|
| Compile-time | Runtime |
| IOException | NullPointerException |
2️⃣7️⃣ What is try-catch-finally?
Used to handle exceptions and ensure cleanup.
2️⃣8️⃣ What is Multithreading?
Executing multiple threads concurrently to improve performance.
2️⃣9️⃣ Difference between Thread and Runnable?
| Thread | Runnable |
|---|---|
| Extends Thread | Implements Runnable |
| Single inheritance | Better design |
3️⃣0️⃣ What is Synchronization?
Prevents multiple threads from accessing shared resources simultaneously.
3️⃣1️⃣ What is Deadlock?
Two or more threads waiting indefinitely for each other’s resources.
3️⃣2️⃣ What is Collection Framework?
A set of classes & interfaces for storing and manipulating groups of objects.
3️⃣3️⃣ Difference between List, Set, and Map?
| Interface | 특징 |
|---|---|
| List | Allows duplicates |
| Set | No duplicates |
| Map | Key-value pairs |
3️⃣4️⃣ Difference between Array and ArrayList?
| Array | ArrayList |
|---|---|
| Fixed size | Dynamic |
| Faster | Flexible |
3️⃣5️⃣ Difference between HashMap and Hashtable?
| HashMap | Hashtable |
|---|---|
| Not synchronized | Synchronized |
| Allows null | No null |
3️⃣6️⃣ What is Hashing?
Converting an object into a fixed-size hash value.
3️⃣7️⃣ What is Comparable?
Used for natural ordering.
3️⃣8️⃣ What is Comparator?
Used for custom sorting logic.
3️⃣9️⃣ What is Lambda Expression?
A short syntax for writing anonymous functions.
4️⃣0️⃣ What is Stream API?
Used for functional-style operations on collections.
4️⃣1️⃣ What is Optional?
A container to avoid NullPointerException.
4️⃣2️⃣ What is Serialization?
Converting an object into a byte stream.
4️⃣3️⃣ What is Deserialization?
Converting byte stream back into an object.
4️⃣4️⃣ What is Marker Interface?
An empty interface used to provide metadata (e.g., Serializable).
4️⃣5️⃣ What is Reflection?
Ability to inspect and modify classes at runtime.
4️⃣6️⃣ What is transient keyword?
Prevents a variable from being serialized.
4️⃣7️⃣ What is volatile keyword?
Ensures visibility of shared variables across threads.
4️⃣8️⃣ What is Java Memory Model?
Defines how threads interact through memory (heap, stack).
4️⃣9️⃣ What is Heap vs Stack?
| Heap | Stack |
|---|---|
| Objects | Method calls |
| Shared | Thread-local |
5️⃣0️⃣ Why Java is still popular?
-
Huge ecosystem
-
Enterprise ready
-
Strong community
-
Platform independent
-
Constant evolution (Java 8 → 21+)
Final Interview Tip
Understand concepts, not definitions.
Most interviewers test how and why, not just what.
No comments:
Post a Comment