0% found this document useful (0 votes)
19 views4 pages

Java8 Lambda Optional

The document provides detailed answers to common questions about Java 8 features, particularly focusing on lambda expressions and the Optional class. Key points include the rules for lambda expressions, the significance of functional interfaces, and the advantages of using Optional to avoid null checks. It also discusses the new Date-Time API, immutability, and how to handle time zones in Java 8.

Uploaded by

krishna das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Java8 Lambda Optional

The document provides detailed answers to common questions about Java 8 features, particularly focusing on lambda expressions and the Optional class. Key points include the rules for lambda expressions, the significance of functional interfaces, and the advantages of using Optional to avoid null checks. It also discusses the new Date-Time API, immutability, and how to handle time zones in Java 8.

Uploaded by

krishna das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java 8 Lambda & Optional QnA - Detailed Answers

Q1. Can a lambda expression be assigned to a variable?


Yes, a lambda expression can be assigned to a variable. The variable type must be a functional interface,
which is an interface with exactly one abstract method.

Q2. If a lambda captures a local variable, what is the rule about that variable?
The local variable must be final or effectively final. This ensures that the lambda captures a stable value,
avoiding unexpected changes.

Q3. In a lambda, what does this refer to?


Inside a lambda, this refers to the enclosing class instance, not the lambda itself. Unlike anonymous
classes, lambdas do not have their own this .

Q4. Can we create a lambda without any parameters? Give an example.


Yes, you can create a lambda without parameters. Example: Runnable r = () ->
System.out.println("Hello World");

Q5. Main difference between a lambda and an anonymous inner class regarding this
reference?
In a lambda, this refers to the enclosing class. In an anonymous inner class, this refers to the
anonymous class itself.

Q6. Can you directly overload a method using two different lambda expressions? Why or why not?
No, because lambda expressions do not have a unique type by themselves. Overloading depends on
functional interface types, not lambda syntax.

Q7. What are method references and when are they preferred over lambda expressions?
Method references are a shorthand to call existing methods. They are preferred for readability when a
lambda only calls an existing method without extra logic.

Q8. Can you use a constructor reference with arguments? Give an example.
Yes. Example: Function<String, ArrayList<String>> f = ArrayList::new; creates a new
ArrayList using a constructor reference.

Q9. Why do method references always need a functional interface context?


Java needs the functional interface to know the expected method signature and parameter types to
match the reference correctly.

Q10. Can you pass a method reference to a method expecting a Predicate? How?
Yes. Example: list.removeIf(String::isEmpty); passes a method reference to remove empty
strings using Predicate<String>.

Q11. What is a functional interface and why is it important for lambda expressions?
A functional interface has exactly one abstract method. Lambdas can be assigned to them because Java
knows which method the lambda is implementing.

1
Q12. Can an interface with no abstract methods still be considered a functional interface? Why or
why not?
No, a functional interface must have one abstract method. Without it, there’s nothing for the lambda to
implement.

Q13. What happens if you declare two abstract methods in an interface annotated with
@FunctionalInterface?
Compilation error occurs, because @FunctionalInterface enforces exactly one abstract method.

Q14. Can a functional interface have static methods?


Yes. Static methods are allowed but they are not abstract and do not count towards the single abstract
method.

Q15. Why were default methods introduced in interfaces in Java 8?


Default methods provide a method implementation in an interface, allowing new methods in interfaces
without breaking existing implementations.

Q16. Can a class inherit two default methods with the same signature from two interfaces? What
must the class do?
Yes, but the class must override the method to resolve the conflict.

Q17. Can a static method in an interface be called using an implementing class object? Why or
why not?
No. Static methods in interfaces must be called using the interface name, not the object, because they
belong to the interface itself.

Q18. Difference between default methods and abstract methods in interfaces?


Default methods have implementation, abstract methods do not. Abstract methods must be
implemented by the class.

Q19. Why was the Optional class introduced in Java 8?


Optional helps avoid null checks and reduces NullPointerExceptions by providing a container that may
or may not contain a value.

Q20. What happens if you call Optional.of(null)?


It throws a NullPointerException because Optional.of does not accept null.

Q21. Difference between Optional.ofNullable(null) and Optional.empty()?


Both create an empty Optional. ofNullable can accept null safely, whereas empty() explicitly creates an
empty Optional.

Q22. How does orElse() differ from orElseGet() in Optional?


orElse evaluates the default value immediately, while orElseGet takes a Supplier and evaluates lazily
only if needed.

Q23. When would you use ifPresent() instead of isPresent() in Optional?


ifPresent executes a given action if the value exists, making code concise and avoiding manual null
checks.

2
Q24. Why is it not recommended to use Optional as a field in JPA entity classes?
Optional adds overhead and can confuse ORM tools; it’s designed for return types, not fields.

Q25. Difference between LocalDateTime and ZonedDateTime?


LocalDateTime has no timezone. ZonedDateTime includes timezone information.

Q26. Which class in the new Date-Time API would you use to represent machine timestamp
values?
Instant.

Q27. Difference between Period and Duration in Java 8 Date-Time API?


Period is date-based (years, months, days). Duration is time-based (hours, minutes, seconds).

Q28. Is the new Date-Time API immutable?


Yes, all classes are immutable and thread-safe.

Q29. What is the advantage of immutability here?


Prevents unexpected changes and ensures thread safety.

Q30. How do you add 10 days to the current date using Java 8 Date-Time API?
LocalDate.now().plusDays(10) .

Q31. How do you find the number of days between two LocalDate objects?
ChronoUnit.DAYS.between(date1, date2) .

Q32. Can you parse a custom date format (like dd-MM-yyyy) with the new Date-Time API? Which
class would you use?
Yes, using DateTimeFormatter: LocalDate.parse("25-08-2025",
DateTimeFormatter.ofPattern("dd-MM-yyyy")) .

Q33. Difference between Instant.now() and LocalDateTime.now()?


Instant.now() gives UTC timestamp. LocalDateTime.now() gives date-time without timezone.

Q34. Why is java.time API considered thread-safe while java.util.Date was not?
Because all java.time classes are immutable.

Q35. How does the new Date-Time API handle time zones compared to the old API?
Using ZonedDateTime and ZoneId, time zones are explicit and consistent.

Q36. Can you combine Predicates in Java 8? How?


Yes. Example: p1.and(p2).or(p3).negate();

Q37. Can you create your own functional interface? What must it follow?
Yes, must have exactly one abstract method. Can have default or static methods.

Q38. Why can’t lambdas be directly assigned to Object type variables?


Because the compiler needs a functional interface type to know which method the lambda implements.

3
Q39. Can a lambda throw a checked exception? If yes, how?
Yes. The functional interface method must declare the checked exception, and the lambda can throw it.

Q40. Why is Optional not a replacement for null everywhere in Java?


Because using Optional everywhere adds overhead and is not suitable for fields or collections.

Q41. Can you override a default method in an implementing class?


Yes, just provide an implementation in the class, which will override the interface default method.

You might also like