11 November 2020

#CoreJava_08

#CoreJava_08

Key Concepts


Topic Sub-Topics Basic Intermediate Advanced Expert
Lambda Expressions Syntax, Scope, Variable capture, Method references, Constructor references ✔️ ✔️ ✔️ ✔️
Functional Interfaces Predicate, Consumer, Supplier, Function, BiFunction, UnaryOperator, BinaryOperator, Custom FI ✔️ ✔️ ✔️ ✔️
Stream API - Basics Creating streams, map(), filter(), forEach(), count(), collect() ✔️ ✔️
Stream API - Intermediate distinct(), sorted(), limit(), skip(), peek(), Optional + Streams ✔️ ✔️
Stream API - Advanced reduce(), flatMap(), groupingBy(), partitioningBy(), joining(), summarizing, parallelStream() ✔️ ✔️ ✔️
Stream API - Expert Custom collectors, Stream performance tuning, Spliterator, Lazy evaluation, Infinite streams ✔️ ✔️
Optional Class of(), ofNullable(), empty(), isPresent(), ifPresent(), orElse(), orElseGet(), orElseThrow() ✔️ ✔️
Date and Time API (java.time) LocalDate, LocalTime, LocalDateTime, Instant, ZoneId, OffsetDateTime, Period, Duration ✔️ ✔️ ✔️
Date and Time Formatting DateTimeFormatter, Custom patterns, Parsing, Timezone conversions ✔️ ✔️
Default & Static Methods in Interfaces Default methods, Static methods, Diamond problem, Multiple inheritance resolution ✔️ ✔️ ✔️ ✔️
CompletableFuture & Concurrency supplyAsync(), runAsync(), thenApply(), thenAccept(), thenCombine(), allOf(), anyOf() ✔️ ✔️ ✔️
Asynchronous Programming Exception handling in CompletableFuture, Non-blocking computations, Chaining futures ✔️ ✔️
Nashorn JavaScript Engine Script execution, Bind Java Objects, Invoke Java methods, External JS files, Performance ✔️ ✔️
Collections Enhancements forEach(), removeIf(), replaceAll(), computeIfAbsent(), computeIfPresent(), merge() ✔️ ✔️ ✔️
Map API Improvements getOrDefault(), putIfAbsent(), replaceAll(), compute(), replace(), new traversal APIs ✔️ ✔️ ✔️
StringJoiner & Collectors joining(), delimiters, prefix & suffix, Collectors.toList(), toSet(), toMap() ✔️ ✔️ ✔️
Base64 Encoding/Decoding Basic, URL, MIME encoding & decoding ✔️ ✔️
Arrays Enhancements parallelSort(), setAll(), parallelPrefix(), Arrays.stream() ✔️ ✔️ ✔️
Annotations (Java 8) @FunctionalInterface, @Repeatable, Type Annotations, @Target, @Retention ✔️ ✔️ ✔️
Method References Reference to static methods, instance methods, constructors ✔️ ✔️ ✔️
Parallel Programming Parallel streams, ForkJoinPool, Performance considerations ✔️ ✔️ ✔️
Spliterator API tryAdvance(), forEachRemaining(), characteristics(), estimateSize(), parallel iteration ✔️ ✔️
Security & Java 8 SecureRandom with streams, Optional null-safety, Immutable Date-Time API ✔️ ✔️ ✔️ ✔️
Miscellaneous Utilities Objects.requireNonNullElse(), Files.lines(), Instant.now(), Comparator.comparing() ✔️ ✔️ ✔️ ✔️
Performance Tuning Streams vs loops, Parallel stream overhead, Memory considerations ✔️ ✔️
Testing with Java 8 Testing Lambdas, Streams, and CompletableFuture, Mocking functional interfaces ✔️ ✔️ ✔️

Interview question


1. Lambda Expressions (25 Questions)

  1. What is a lambda expression in Java 8?
  2. Why were lambda expressions introduced in Java?
  3. Explain the syntax of a lambda expression with an example.
  4. How does a lambda expression differ from an anonymous class?
  5. Can lambda expressions access local variables from the enclosing scope?
  6. What is variable capture in lambdas?
  7. What are the restrictions on accessing variables inside lambdas?
  8. How does this behave inside a lambda expression?
  9. Can lambdas be recursive? How?
  10. How do you use lambdas with functional interfaces?
  11. What is the target type of a lambda expression?
  12. Can you overload methods with lambda parameters?
  13. How does type inference work in lambda expressions?
  14. Can lambdas throw checked exceptions?
  15. Can lambdas have multiple return statements?
  16. Difference between -> and :: operators in Java 8.
  17. What are the advantages of using lambdas?
  18. How are lambdas internally implemented in Java?
  19. Can lambdas be serialized?
  20. How do you debug lambda expressions?
  21. Can you assign a lambda to a variable?
  22. How do lambdas improve iteration over collections?
  23. Give an example of using a lambda with Runnable.
  24. How does the compiler determine the type of a lambda?
  25. What are some real-world use cases of lambdas?

2. Functional Interfaces (25 Questions)

  1. What is a functional interface in Java 8?
  2. Explain the @FunctionalInterface annotation.
  3. Give 5 examples of built-in functional interfaces.
  4. Difference between Predicate and Function.
  5. Difference between Consumer and Supplier.
  6. What is a UnaryOperator? Example?
  7. What is a BinaryOperator? Example?
  8. How do you chain functional interfaces like Predicate and Function?
  9. Can functional interfaces have default methods?
  10. Can functional interfaces have static methods?
  11. Can a functional interface extend another interface?
  12. What happens if you declare two abstract methods in a functional interface?
  13. How does type inference work with functional interfaces?
  14. Can a functional interface be generic?
  15. What are primitive specializations of functional interfaces?
  16. Can functional interfaces throw exceptions?
  17. How do you implement a custom functional interface?
  18. How do you use method references with functional interfaces?
  19. What is the difference between apply() in Function and test() in Predicate?
  20. Can functional interfaces be Serializable?
  21. What is the role of java.util.function package?
  22. When should you explicitly use @FunctionalInterface?
  23. Can functional interfaces override equals() and hashCode()?
  24. How do functional interfaces enable functional programming in Java?
  25. Real-world use case of functional interfaces?

3. Stream API Basics (25 Questions)

  1. What is the Stream API in Java 8?
  2. Difference between a Collection and a Stream?
  3. How do you create a stream from a list?
  4. What are intermediate operations in streams?
  5. What are terminal operations in streams?
  6. Explain lazy evaluation in streams.
  7. Difference between map() and flatMap().
  8. What does the filter() operation do?
  9. How do you sort a stream?
  10. What is the use of forEach() in streams?
  11. Difference between sequential and parallel streams.
  12. How do you collect stream results into a list?
  13. How do you collect stream results into a set?
  14. What is the reduce() operation in streams?
  15. Difference between findFirst() and findAny().
  16. How do you use limit() and skip() in streams?
  17. How does distinct() work in streams?
  18. Explain peek() and its use case.
  19. Can you reuse a stream after a terminal operation?
  20. How do you create an infinite stream?
  21. How does performance differ between loops and streams?
  22. Can streams work on primitive types?
  23. How do you debug a stream pipeline?
  24. How does short-circuiting work in streams?
  25. Real-world use case of streams?

4. Optional Class (25 Questions)

  1. What is Optional in Java 8?
  2. Why was Optional introduced?
  3. How do you create an Optional object?
  4. Difference between Optional.of() and Optional.ofNullable().
  5. What does Optional.empty() return?
  6. How do you check if a value is present in Optional?
  7. Difference between isPresent() and ifPresent().
  8. How do you provide a default value with Optional?
  9. Difference between orElse() and orElseGet().
  10. Difference between orElse() and orElseThrow().
  11. How do you transform values inside Optional using map()?
  12. Difference between map() and flatMap() in Optional.
  13. How do you filter values in Optional?
  14. Can Optional itself be null?
  15. Can Optional be used for method parameters?
  16. What are drawbacks of using Optional excessively?
  17. Can Optional be serialized?
  18. How do you return Optional from repository methods?
  19. Can you use Optional with Streams?
  20. How do you combine multiple Optionals?
  21. Is Optional thread-safe?
  22. Can Optional hold collections?
  23. What is the difference between Optional in Java and Maybe in other FP languages?
  24. Should Optional be used in DTOs?
  25. Real-world use cases of Optional?

5. Date and Time API (25 Questions)

  1. What is the new Date/Time API in Java 8?
  2. Why was it introduced when we already had Date and Calendar?
  3. What is LocalDate?
  4. What is LocalTime?
  5. What is LocalDateTime?
  6. What is ZonedDateTime?
  7. How do you parse and format dates in Java 8?
  8. Difference between DateTimeFormatter and SimpleDateFormat?
  9. What is Period in Java 8?
  10. What is Duration in Java 8?
  11. How do you calculate the difference between two dates?
  12. How do you add or subtract days from a date?
  13. How do you check if a year is a leap year?
  14. How do you handle time zones in Java 8?
  15. Difference between Instant and LocalDateTime.
  16. How do you convert between old Date and new DateTime API?
  17. What is Clock in Date/Time API?
  18. What is TemporalAdjuster?
  19. What is TemporalUnit?
  20. How do you get the start and end of a day?
  21. What is the difference between OffsetDateTime and ZonedDateTime?
  22. How do you compare two dates in Java 8?
  23. Can LocalDate represent time?
  24. Can LocalTime represent date?
  25. Real-world use cases of Date/Time API?

6. Default & Static Methods in Interfaces (25 Questions)

  1. What are default methods in Java 8?
  2. Why were default methods introduced?
  3. Can interfaces have method implementations in Java 8?
  4. How do you define a default method?
  5. Can a class override a default method?
  6. What happens if two interfaces have the same default method?
  7. How do you resolve conflicts in multiple inheritance with default methods?
  8. Can interfaces have static methods?
  9. How do you call a static method in an interface?
  10. Can default methods be abstract?
  11. Can interfaces have private methods in Java 8?
  12. How do default methods impact backward compatibility?
  13. Can constructors be default methods?
  14. How do you use default methods with collections?
  15. Can default methods call abstract methods?
  16. Can default methods be overridden by subclasses?
  17. What is the difference between default methods and abstract classes?
  18. Can default methods be synchronized?
  19. Can default methods throw exceptions?
  20. Can default methods call other default methods?
  21. Are static methods inherited in interfaces?
  22. Can you override static methods from interfaces?
  23. Can static methods in interfaces be private?
  24. Real-world use case of default methods?
  25. Real-world use case of static methods in interfaces?

7. CompletableFuture & Concurrency (25 Questions)

  1. What is CompletableFuture in Java 8?
  2. How is CompletableFuture different from Future?
  3. How do you create a CompletableFuture?
  4. How do you run asynchronous tasks with CompletableFuture?
  5. Difference between runAsync() and supplyAsync().
  6. What is thenApply() in CompletableFuture?
  7. What is thenAccept()?
  8. What is thenRun()?
  9. Difference between thenCompose() and thenCombine().
  10. How do you handle exceptions in CompletableFuture?
  11. What is exceptionally() method?
  12. What is handle() method?
  13. How do you combine multiple CompletableFutures?
  14. Difference between allOf() and anyOf().
  15. How do you cancel a CompletableFuture?
  16. How do you block and get the result?
  17. What is join() in CompletableFuture?
  18. Difference between join() and get().
  19. How do you implement timeouts in CompletableFuture?
  20. Is CompletableFuture thread-safe?
  21. How do you use custom thread pools with CompletableFuture?
  22. Difference between synchronous and asynchronous callbacks?
  23. How does CompletableFuture improve parallelism?
  24. How do you test CompletableFutures?
  25. Real-world use cases of CompletableFuture?

8. Collections & Map Enhancements (25 Questions)

  1. What enhancements were made to collections in Java 8?
  2. What is the forEach() method in collections?
  3. How do you use removeIf()?
  4. What is the replaceAll() method?
  5. What is the sort() method in List?
  6. How do you use computeIfAbsent() in Map?
  7. How do you use computeIfPresent()?
  8. How do you use compute()?
  9. What is merge() in Map?
  10. How do you use getOrDefault() in Map?
  11. What is putIfAbsent()?
  12. What is replace() in Map?
  13. How do you use forEach() in Map?
  14. Difference between entrySet() iteration and forEach().
  15. How do you sort a Map by values?
  16. Can you use streams with collections directly?
  17. What is Spliterator?
  18. How is Spliterator different from Iterator?
  19. What is trySplit() in Spliterator?
  20. How do you estimate size in Spliterator?
  21. What is the ORDERED characteristic in Spliterator?
  22. What is the CONCURRENT characteristic?
  23. What is the DISTINCT characteristic?
  24. What is the IMMUTABLE characteristic?
  25. Real-world use case of collection enhancements?

9. Streams Advanced (25 Questions)

  1. What are parallel streams?
  2. How do you create a parallel stream?
  3. Difference between sequential and parallel stream performance?
  4. What are the pitfalls of parallel streams?
  5. How does fork-join framework work with parallel streams?
  6. What is Collectors.groupingBy()?
  7. What is Collectors.partitioningBy()?
  8. Difference between groupingBy() and partitioningBy().
  9. What is Collectors.mapping()?
  10. How do you join strings using Collectors.joining()?
  11. How do you count elements using Collectors.counting()?
  12. What is Collectors.summarizingInt()?
  13. What is Collectors.averagingDouble()?
  14. What is Collectors.reducing()?
  15. What is a custom collector?
  16. How do you implement your own collector?
  17. What is a downstream collector?
  18. Difference between reduce() and collect().
  19. Can streams be infinite?
  20. How do you use Stream.iterate()?
  21. How do you use Stream.generate()?
  22. What is flatMapToInt()?
  23. What is boxed() in streams?
  24. What is parallelism level in parallel streams?
  25. Best practices for using advanced streams?

10. Miscellaneous Java 8 Features (25 Questions)

  1. What is Nashorn in Java 8?
  2. How do you execute JavaScript with Nashorn?
  3. What is StringJoiner in Java 8?
  4. How do you join strings with a delimiter using StringJoiner?
  5. What enhancements were made to String in Java 8?
  6. What is the new Base64 API in Java 8?
  7. Difference between Encoder and Decoder in Base64.
  8. How do you encode and decode a string with Base64?
  9. What enhancements were made to Arrays in Java 8?
  10. How do you sort an array with a custom comparator in Java 8?
  11. What is Arrays.parallelSort()?
  12. What is SplittableRandom?
  13. How is SplittableRandom different from Random?
  14. What enhancements were made to Comparator in Java 8?
  15. How do you use comparing() method in Comparator?
  16. How do you use thenComparing()?
  17. What are annotation enhancements in Java 8?
  18. What is repeating annotations?
  19. What is type annotation in Java 8?
  20. How do you declare type-use annotations?
  21. Can annotations be applied to generics in Java 8?
  22. Can annotations be inherited in Java 8?
  23. What is method parameter reflection in Java 8?
  24. What are compact profiles?
  25. Summary of miscellaneous features introduced in Java 8?

Related Topics


   Lambda Expressions   
   Functional Interfaces   
   Stream API