100 interview questions for Java 8


 

Here are 100 Java 8 interview questions, organized by level:


Beginner (30 Questions)

  1. What are the major features introduced in Java 8?

  2. What is a lambda expression? Write its basic syntax.

  3. What makes an interface a functional interface, and how do you annotate one?

  4. What are default methods in interfaces, and why were they added?

  5. How do static methods in interfaces work? Give an example.

  6. What is the Stream API, and why use it over traditional loops?

  7. Explain the difference between intermediate and terminal operations in a Stream.

  8. How do you filter elements in a Stream?

  9. How do you map elements in a Stream?

  10. How do you sort elements in a Stream pipeline?

  11. How do you collect Stream elements into a List using Collectors.toList()?

  12. What is the Optional class, and how does it help avoid null checks?

  13. How do you create an Optional with and without a value?

  14. How do you use Optional.ifPresent()?

  15. What’s the difference between Optional.orElse() and orElseGet()?

  16. Name the main classes introduced in the Date/Time API (java.time).

  17. How do you get the current date with LocalDate.now()?

  18. How do you parse a string into a LocalDate?

  19. How do you format a LocalDate to a string?

  20. What is the difference between LocalDate, LocalTime, and LocalDateTime?

  21. What are Duration and Period used for?

  22. How do you measure the duration between two Instants?

  23. What is a method reference? List three types with examples.

  24. How do you perform Base64 encoding and decoding in Java 8?

  25. What’s the purpose of the java.util.function package? Name three interfaces it provides.

  26. How do you create a Stream from a Collection?

  27. How do you create a Stream from an array?

  28. What does Collectors.joining() do? Give an example.

  29. How do you implement Runnable using a lambda expression?

  30. What is Stream.of() and when would you use it?


Medium (40 Questions)

  1. Explain the difference between map() and flatMap() in Streams.

  2. What is the reduce() operation? Provide a sample use-case.

  3. How do you group elements using Collectors.groupingBy()?

  4. How do you partition a Stream with Collectors.partitioningBy()?

  5. Outline the steps to build a custom Collector.

  6. How do you create infinite Streams with Stream.iterate() and Stream.generate()?

  7. When and how would you use parallelStream()? What should you watch out for?

  8. What is a Spliterator, and how does it relate to Streams?

  9. How do you manually split work with a Spliterator?

  10. What Spliterator characteristics affect parallel performance?

  11. Give examples of short-circuiting Stream operations.

  12. What’s the difference between forEach() and forEachOrdered()?

  13. How can you handle exceptions inside a lambda?

  14. How do you chain multiple Comparators?

  15. What do Comparator.nullsFirst() and nullsLast() do?

  16. How do you convert a Stream to a Map with collectors?

  17. What happens if you collect into a concurrent map?

  18. How do you use Collector.of() to create a stateful collector?

  19. How does Collectors.collectingAndThen() work?

  20. Explain Optional.map() vs. Optional.flatMap().

  21. How and why use Optional.orElseThrow()?

  22. How do you use DateTimeFormatter with custom patterns?

  23. How do you convert between LocalDateTime and a different time zone (ZonedDateTime)?

  24. How do you convert legacy java.util.Date to java.time.LocalDate?

  25. How do you combine multiple CompletableFutures?

  26. What’s the difference between thenApply(), thenCompose(), and thenCombine()?

  27. How do you handle errors in a CompletableFuture chain?

  28. What are CompletableFuture.allOf() and anyOf() for?

  29. How do default methods resolve conflicts when two interfaces define the same method?

  30. How do you use LongAdder and LongAccumulator for high-throughput counters?

  31. Compare Collections.synchronizedList() vs. List.of() (immutable list).

  32. What new methods does java.nio.file.Path provide?

  33. How do you watch a directory for file events using WatchService?

  34. How do you execute JavaScript with the Nashorn engine?

  35. How would you implement a simple memoization helper using Java 8 features?

  36. How do you profile and debug a long-running Stream pipeline?

  37. Explain the role of Supplier, Consumer, Function, and Predicate in the java.util.function package.

  38. What is Stream.peek() and when is it useful (or dangerous)?

  39. How does Collection.removeIf() differ from Stream.filter()?

  40. How do you use Optional to safely navigate nested properties (avoid NPEs)?


Advanced (30 Questions)

  1. How do you implement a custom Spliterator for a domain-specific data structure?

  2. Walk through building a complex Collector with Collector.of(), including UNORDERED or CONCURRENT characteristics.

  3. What pitfalls arise from stateful intermediate operations in parallel Streams?

  4. Describe how the ForkJoinPool underpins parallelStream() execution.

  5. How would you tune parallel Stream performance for very large data sets?

  6. How do you supply a custom Executor to a CompletableFuture?

  7. How do you enforce timeouts in CompletableFuture workflows?

  8. Compare handle(), exceptionally(), and whenComplete() for error handling in CompletableFuture.

  9. How can you implement backpressure or flow control using Java 8 constructs?

  10. How do you support cancellation in chained CompletableFutures?

  11. Implement a Collector that dynamically buckets data based on a runtime criterion.

  12. Simulate sliding-window aggregations (time-based) using Streams.

  13. How do you use MethodHandle to invoke a private method reflectively?

  14. What is invokedynamic, and how did Java 8 improve dynamic language support?

  15. How would you implement a tail-recursive algorithm in Java 8 (without native tail calls)?

  16. Design a mini-DSL using functional interfaces and default methods.

  17. How do you leverage type annotations (@Target(ElementType.TYPE_USE)) introduced in Java 8?

  18. How do repeatable annotations work, and how do you access them via reflection?

  19. Create and use a custom annotation processor in Java 8.

  20. Build bi-directional Java↔JavaScript interop logic using the Nashorn engine.

  21. Convert a Stream<CompletableFuture> into CompletableFuture<List<T>>.

  22. How do you correctly parallelize nested Stream pipelines?

  23. Implement a lock-free, thread-safe data structure using AtomicReference or Atomic* classes.

  24. Use StampedLock for optimistic read/write patterns.

  25. Create a dynamic proxy via the LambdaMetafactory.

  26. Optimize GC pressure when working with large Streams and avoid memory leaks.

  27. Architect a reactive event-driven framework using core Java 8 features.

  28. Migrate a legacy callback-based API to a CompletableFuture-based design.

  29. Use Optional and Streams to build fluent guard clauses in your code.

  30. How do you write unit tests for complex Stream and CompletableFuture pipelines using JUnit and Mockito?


Good luck with your Java 8 interview prep!

Comments