Core Java Interview Notes - Part 3 (Java 8+ + Collections +
Threads)
Final Keyword
Q) What is final keyword in Java?
A) final means cannot change. final variable cannot be reassigned, final method cannot be overridden, final class
cannot be inherited.
Q) Common use cases of final variables?
A) Used for constants, method parameters, and local variables in lambdas.
Q) How final helps immutability and thread safety?
A) Because value cannot change after assignment, so it avoids unexpected modifications across threads.
Q) Performance consideration of final?
A) JVM can optimize final values, so sometimes it improves performance slightly.
Functional Interface & Java 8
Q) What is functional interface?
A) Interface with only one abstract method. Used for lambda expressions.
Q) Can functional interface extend another interface?
A) Yes, if it does not add extra abstract methods (only default/static allowed).
Q) Advantages of functional interface?
A) Enables lambda expressions, clean code, better readability, and stream operations.
Q) Java 8 new features?
A) Lambda, Streams, Method references, Default methods, Optional, New Date-Time API.
Q) Why Optional, Lambda, Streams introduced?
A) Optional reduces null issues, Lambda reduces boilerplate code, Streams make collection processing easy.
Q) filter vs map?
A) filter removes elements based on condition. map transforms each element.
Java 11 / 17 / 21 Features
Q) Java 11 features?
A) HTTP Client API, Epsilon GC, ZGC, and new String methods like isBlank(), strip(), repeat().
Q) Java 17 features?
A) Sealed classes, pattern matching improvements, foreign function/memory API (preview).
Q) Java 21 features?
A) Virtual threads, structured concurrency, scoped values, sequenced collections, record patterns.
Streams Concepts
Q) For loop vs Streams: which is faster?
A) For loop is usually faster due to less overhead. Streams are cleaner and support parallelism.
Q) When to use loops vs streams?
A) Loops for small/simple and performance-critical tasks. Streams for complex transformations and readability.
Q) Intermediate vs terminal operations?
A) Intermediate returns stream and is lazy (filter/map). Terminal gives output and triggers execution
(collect/forEach).
Q) Interface changes from Java 7 to Java 8?
A) Java 8 added default and static methods to interfaces.
Q) Use of [Link]()?
A) Joins strings using delimiter. Example: [Link](",", "A", "B") -> A,B
Collections Framework
Q) What is Collection Framework?
A) Set of interfaces and classes to store and manage group of objects.
Q) Main interfaces in collection framework?
A) Collection, List, Set, Queue, Map.
Q) What is Iterator?
A) Used to traverse collection elements one by one.
Q) Common methods in Collection?
A) add(), remove(), clear(), size(), isEmpty().
Q) Concurrency support in collections?
A) Uses thread-safe classes like ConcurrentHashMap and CopyOnWriteArrayList.
Q) How to choose collection type?
A) List for ordered+duplicates, Set for unique, Queue for processing order, Map for key-value.
Q) Java 8 enhancements?
A) Streams and lambda support for bulk operations.
Q) Iterator vs ListIterator?
A) Iterator moves forward only. ListIterator supports forward/backward and modifications.
Q) Sorting algorithm used in [Link] and [Link]?
A) [Link] uses Dual-Pivot QuickSort for primitives and TimSort for objects. [Link] uses TimSort.
Q) Preserve insertion order in Set?
A) Use LinkedHashSet.
Q) Store elements in sorted order?
A) Use TreeSet or TreeMap.
Q) Use case of ArrayList, LinkedList, HashSet?
A) ArrayList for fast access, LinkedList for frequent insert/delete, HashSet for unique fast lookup.
Q) How HashSet avoids duplicates?
A) Uses HashMap internally, elements stored as keys.
Q) hashCode and equals work together?
A) hashCode finds bucket, equals checks exact match inside bucket.
Q) Why override hashCode when overriding equals?
A) To ensure HashMap/HashSet store and retrieve objects correctly.
Q) When TreeSet better than HashSet?
A) When sorted order is needed.
Q) Internal structure of ArrayList and LinkedList?
A) ArrayList uses dynamic array, LinkedList uses doubly linked list.
HashMap & ConcurrentHashMap
Q) Internal working of HashMap?
A) Stores key-value pairs in buckets. Uses hashCode to find bucket. Collisions handled by list/tree.
Q) What if two keys have same hashCode?
A) Collision happens. HashMap stores both and uses equals() to differentiate.
Q) How collisions handled in Java 8?
A) If bucket becomes too large, linked list converts into balanced tree for faster search.
Q) Java 8 change in HashMap?
A) Treeification of buckets to improve performance.
Q) Can objects be key in HashMap?
A) Yes, if equals() and hashCode() are properly implemented.
Q) What is ConcurrentHashMap?
A) Thread-safe map allowing concurrent access without locking whole map.
Q) How ConcurrentHashMap improves performance?
A) Different parts can be updated by different threads at same time, reducing locking.
Time Complexity
Q) HashMap/HashSet complexity?
A) Average O(1) insert/delete/search. Worst O(n) if too many collisions.
Q) TreeMap/TreeSet complexity?
A) O(log n) for insert/delete/search.
Q) Internal techniques used?
A) HashMap uses array+list/tree. TreeMap uses Red-Black Tree. HashSet uses HashMap. TreeSet uses TreeMap.
Design Patterns
Q) What is design pattern?
A) Reusable solution for common software design problems.
Q) Common design patterns?
A) Singleton, Observer, Factory Method.
Q) Impact on performance?
A) May add slight overhead but improves maintainability and architecture.
Q) Pattern for database connections?
A) Singleton pattern is commonly used.
HashMap vs TreeMap
Q) Difference between HashMap and TreeMap?
A) HashMap is unordered and faster. TreeMap keeps keys sorted and is O(log n).
Q) When TreeMap preferred?
A) When sorted keys are required.
Q) Can objects be key in TreeMap?
A) Yes, if Comparable is implemented or Comparator is provided.
SOLID Principles
Q) What is SOLID?
A) Five design principles for clean code: SRP, OCP, LSP, ISP, DIP.
Q) Single Responsibility Principle (SRP)?
A) One class should have only one job.
Q) Open/Closed Principle (OCP)?
A) Open for extension but closed for modification.
Q) Liskov Substitution Principle (LSP)?
A) Child objects should replace parent objects without breaking program.
Q) Interface Segregation Principle (ISP)?
A) Split large interfaces into smaller ones.
Q) Dependency Inversion Principle (DIP)?
A) Depend on interfaces/abstractions, not concrete classes.
Threads & Concurrency
Q) What is thread?
A) Thread is a separate path of execution in a program.
Q) How to create thread?
A) Extend Thread class or implement Runnable interface.
Q) Thread lifecycle?
A) New, Runnable, Running, Waiting/Blocked, Terminated.
Q) How to handle shared data updates?
A) Use synchronized, locks, volatile, or concurrent collections.
Q) Can we start thread twice?
A) No, it throws IllegalThreadStateException.
Q) Thread vs Runnable?
A) Runnable is better because class can extend another class too.
Q) How to make method thread-safe?
A) Use synchronized blocks, volatile, atomic variables, or thread-safe collections.
Q) What is volatile?
A) Ensures latest value is always read from main memory.
Q) Thread synchronization?
A) Controls multiple threads accessing shared resource to avoid inconsistency.
Q) wait() and notify() use case?
A) Used in producer-consumer scenario for communication.
Q) Multithreading challenges?
A) Deadlock, race condition, resource contention, debugging difficulty.
Q) Java Memory Model (JMM)?
A) Defines rules for memory visibility and consistency between threads.
Server in Java
Q) Can we create server without Spring?
A) Yes, using Java SE APIs like ServerSocket (TCP server) or HttpServer (HTTP server).