50 java interviews questions

Great! Below is your Java Interview Questions Blog Post converted to Markdown format — ready to publish on your blog (WordPress, GitHub Pages, Medium, or any markdown-supporting platform).




# 🔥 Top 50 Java Interview Questions and Answers [2025 Edition]


Whether you're a **beginner**, **intermediate**, or **experienced Java developer**, these **top Java interview questions** will help you crack coding rounds and technical interviews at companies like TCS, Infosys, Capgemini, and product-based firms.


---


## ✅ Core Java – Basic Level


### 1. What is the difference between JDK, JRE, and JVM?

- **JDK**: Java Development Kit – for developing Java applications.

- **JRE**: Java Runtime Environment – for running Java programs.

- **JVM**: Java Virtual Machine – runs Java bytecode.


### 2. What are the main OOP concepts in Java?

- Inheritance

- Encapsulation

- Abstraction

- Polymorphism


### 3. What is the difference between `==` and `.equals()` in Java?

- `==` compares object references.

- `.equals()` compares values (if overridden).


### 4. What is the default value of a boolean variable?

- `false`


### 5. What is a constructor in Java?

- A special method used to initialize objects.

- Same name as the class.

- No return type.


---


## 🔄 Java – Intermediate Level


### 6. What is method overloading vs overriding?

- **Overloading**: Same method name, different parameters.

- **Overriding**: Redefining a method in the child class.


### 7. Difference between `ArrayList` and `LinkedList`?

- `ArrayList`: Fast for read, slow for insert/delete.

- `LinkedList`: Fast for insert/delete, slow for random access.


### 8. What is the use of `final` keyword?

- `final` class: can’t be extended.

- `final` method: can’t be overridden.

- `final` variable: value can't change.


### 9. What is a static block?

```java

static {

   System.out.println("Static block called");

}


10. What are checked and unchecked exceptions?



  • Checked: Must be handled at compile time (e.g., IOException).
  • Unchecked: Runtime exceptions (e.g., NullPointerException).






🔁 Collections Framework




11. Difference between HashMap and Hashtable?



  • HashMap is non-synchronized.
  • Hashtable is synchronized (thread-safe, but slower).




12. Difference between List and Set?



  • List: allows duplicates, maintains insertion order.
  • Set: no duplicates, no guaranteed order.




13. How does HashMap work internally?



  • Uses hashCode() and equals() to store key-value pairs in buckets.




14. What is Comparable vs Comparator?



  • Comparable: natural ordering (compareTo()).
  • Comparator: custom ordering (compare()).




15. What is fail-fast vs fail-safe?



  • Fail-fast: throws ConcurrentModificationException (e.g., ArrayList).
  • Fail-safe: avoids errors (e.g., CopyOnWriteArrayList).






🔄 Multithreading




16. Ways to create threads in Java?



  • Extend Thread class
  • Implement Runnable
  • Use ExecutorService




17. Difference between sleep() and wait()?



  • sleep(): pauses thread without releasing lock.
  • wait(): pauses and releases lock.




18. What is synchronized keyword?



  • Prevents concurrent access to blocks/methods.




19. What is volatile keyword?



  • Ensures visibility of variable changes across threads.




20. What is deadlock in Java?



  • Two threads wait for each other’s lock indefinitely.






🆕 Java 8 Features




21. What is a lambda expression?


(int a, int b) -> a + b


22. What is a functional interface?


@FunctionalInterface

interface MyFunction {

   void apply();

}


23. What is Stream API?



  • A new abstraction to process collections using functional-style code.




24. map() vs flatMap()?



  • map(): transforms elements.
  • flatMap(): flattens nested elements.




25. What is Optional?


Optional.ofNullable(name).orElse("Default");





💾 Memory & Garbage Collection




26. Heap vs Stack memory?



  • Heap: for objects.
  • Stack: for method calls/local variables.




27. What is garbage collection?



  • JVM’s way of cleaning unused objects automatically.




28. How to make an object eligible for GC?



  • Remove all references.




29. What is finalize()?



  • Called before GC (not recommended for use).




30. What is memory leak?



  • Memory that’s no longer needed but not released.






🔐 Advanced Java Concepts




31. What is 

transient

 keyword?



  • Prevents field from being serialized.




32. Difference between 

this

 and 

super

?



  • this: refers to current class.
  • super: refers to parent class.




33. What is Java Reflection?



  • Allows runtime inspection/modification of classes, methods, etc.




34. Use of enums?



  • Special class to define constants.




35. What is serialization and deserialization?



  • Convert object to byte stream and vice versa.






📦 Design Patterns & Architecture




36. Singleton Design Pattern


private static final Singleton INSTANCE = new Singleton();


37. Factory Pattern



  • Used to create objects without exposing instantiation logic.




38. Builder Pattern



  • For constructing complex objects step-by-step.




39. Dependency Injection



  • Letting frameworks inject required dependencies.




40. Inversion of Control (IoC)



  • Shifts object creation control to the container (e.g., Spring).






💡 Miscellaneous




41. Why is Java platform-independent?



  • Bytecode runs on any OS with JVM.




42. Access modifiers: public, private, protected?



  • Control visibility of classes and methods.




43. What are annotations?


@Override

@Deprecated


44. Abstract class vs Interface?



  • Interface (Java 7): only abstract methods.
  • Abstract: can have method body.




45. What is method reference?


System.out::println





🚀 Bonus Coding Questions




46. Reverse a string:


new StringBuilder(str).reverse().toString();


47. First non-repeating character in string?


// Count frequency, then scan string for first char with freq = 1


48. HashSet vs TreeSet?



  • HashSet: unordered, faster.
  • TreeSet: ordered (sorted), slower.




49. Palindrome check:


boolean isPalindrome(String s) {

  return s.equals(new StringBuilder(s).reverse().toString());

}

Comments

Popular posts from this blog

OBJECT ORIENTED CONCEPTS

Arrays programms