Q.1: What are the major new features in Java 8?
Ans: Java 8 introduced several new features, including
lambdas, functional interfaces, the Stream API, default methods in interfaces,
and the Date and Time API.
Q.2: What is a lambda expression in Java 8?
Ans: A lambda expression is a lightweight syntax for
creating anonymous functions in Java. It can be used to pass behavior as an
argument to a method, or to define a method that can be passed as an argument
to another method.
Q.3: What is a functional interface in Java 8?
Ans: A functional interface is an interface that has exactly
one abstract method. It is used to define a contract for a lambda expression or
method reference. The @FunctionalInterface annotation can be used to enforce the
single abstract method constraint.
Q.4: What is the Stream API in Java 8?
Ans: The Stream API is a new API in Java 8 that allows for
functional-style processing of collections. It provides a declarative way to
perform operations like filtering, mapping, and reducing on a collection.
Q.5: What are default methods in interfaces in Java 8?
Ans: Default methods in interfaces are methods that have an
implementation, but can be overridden by implementing classes. They were added
to interfaces in Java 8 to allow for evolution of interfaces without breaking
existing code.
Q.6: What is the Date and Time API in Java 8?
Ans: The Date and Time API is a new API in Java 8 for
working with dates, times, and time zones. It provides a more comprehensive and
flexible way to work with dates and times than the previous Date and Calendar
APIs.
Q.7: What is the difference between the java.util.Date and
java.time.LocalDate classes?
Ans: The java.util.Date class represents a point in time,
while the java.time.LocalDate class represents a date without a time. The LocalDate
class is also immutable and thread-safe, while the Date class is mutable and
not thread-safe.
Q.8: How do you create a stream in Java 8?
Ans: You can create a stream in Java 8 by calling the
stream() method on a collection, or by using the of() or generate() methods of
the Stream class. You can also create a stream from an array using the
Arrays.stream() method.
Q.9: What is method reference in Java 8?
Ans: A method reference is a shorthand syntax for creating a
lambda expression that calls a method. It can be used to pass a method as an
argument to another method, or to define a lambda expression that calls a
method.
Q.10: What is the difference between a Consumer and a
Supplier in Java 8?
Ans: A Consumer is a functional interface that takes a
single argument and returns no result, while a Supplier is a functional
interface that takes no arguments and returns a result. They are both used in
the Stream API for processing elements of a stream.
0 Comments