Java 8 Interview Questions & Answers
1. Comparable vs Comparator
- Comparable: Interface with compareTo(); natural ordering; implemented in the class.
- Comparator: Interface with compare(); custom ordering; defined outside the class.
2. StringBuffer vs StringBuilder
- StringBuffer: Mutable, thread-safe (synchronized).
- StringBuilder: Mutable, not thread-safe (faster).
3. Checked & Unchecked Exceptions
- Checked: Compile-time check; must handle or declare. Example: IOException.
- Unchecked: Runtime check; RuntimeException subclasses. Example: NullPointerException.
4. equals() & hashCode()
- equals(): Compares object equality.
- hashCode(): Returns hash for hash-based collections.
- Rule: Equal objects must have the same hash code.
5. Hashtable vs HashMap
- Hashtable: Synchronized, no null key, slower.
- HashMap: Not synchronized, one null key allowed, faster.
6. ArrayList vs Vector
- ArrayList: Not synchronized, faster.
- Vector: Synchronized, legacy.
7. Abstraction vs Interface
- Abstraction: Hide implementation; abstract classes/methods.
- Interface: Contract; all methods abstract until Java 8.
8. ArrayList vs LinkedList
- ArrayList: Dynamic array, fast random access, slow middle insert/delete.
- LinkedList: Doubly-linked, fast insert/delete, slow random access.
9. Method Reference (Java 8)
- Static method: ClassName::methodName
- Instance method: instance::methodName
- Constructor: ClassName::new
10. Inbuilt Functional Interfaces
- Predicate<T>, Function<T,R>, Consumer<T>, Supplier<T>.
11. Default Sizes
- ArrayList: 10
- HashMap: Capacity=16, Load factor=0.75
12. OOPS Pillars & Advantages
- Encapsulation: Data hiding; getters/setters.
- Inheritance: Code reuse; extends.
- Polymorphism: Many forms; overloading/overriding.
- Abstraction: Hide details; abstract classes.
13. JDK vs JRE vs JVM
- JDK: Development kit (compiler + tools + JRE).
- JRE: Runtime environment.
- JVM: Executes bytecode.
14. String Reverse, Palindrome, Fibonacci
- Reverse: Loop or [Link]().
- Palindrome: Compare string to reverse.
- Fibonacci: F(n) = F(n-1) + F(n-2).
15. Basic String Problems
- Count vowels, duplicates, anagram, remove spaces.
16. Access Modifiers
- private, default, protected, public.
17. Multiple Try-Catch Blocks
- Handle different exceptions separately.
18. Object Class Methods
- toString(), hashCode(), equals(), clone(), finalize(), getClass(), wait(), notify().
19. String, Encryption, Reflection
- String: Immutable, String pool.
- Encryption: AES, RSA.
- Reflection: Inspect/modify classes at runtime.