0% found this document useful (0 votes)
4 views

Java_8_Interview_Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java_8_Interview_Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Most Asked Java 8 Interview Questions

Key Features of Java 8

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

Lambda Expressions, Functional Interfaces, Stream API, Default Methods, Method References,

Optional, New Date/Time API, Nashorn JavaScript Engine.

2. What are Lambda Expressions in Java 8?

Lambda expressions allow you to express instances of single-method interfaces (functional

interfaces) concisely using an expression. It eliminates the need for anonymous class

implementations.

Example:

@FunctionalInterface

interface Greeting {

void greet(String name);

public class LambdaExample {

public static void main(String[] args) {

Greeting greet = (name) -> System.out.println("Hello " + name);

greet.greet("John");

3. What is a Functional Interface?

A functional interface has only one abstract method, and it may contain multiple default or static

methods. It is meant to be used with lambda expressions.


Example:

@FunctionalInterface

interface Converter {

int convert(String s);

public class FunctionalInterfaceExample {

public static void main(String[] args) {

Converter converter = (str) -> Integer.parseInt(str);

System.out.println(converter.convert("123"));

4. What is the Stream API?

The Stream API enables functional-style operations on streams of elements, such as collections. It

allows you to process elements in a sequence efficiently, providing operations like map, filter, and

reduce.

Example:

import java.util.*;

import java.util.stream.*;

public class StreamExample {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

int sum = numbers.stream()


.filter(n -> n % 2 == 0)

.mapToInt(n -> n)

.sum();

System.out.println(sum);

5. What is the purpose of the Optional class?

Optional is used to represent values that may or may not be present, avoiding

NullPointerExceptions. It provides methods like isPresent, ifPresent, and orElse to handle null

values safely.

Example:

import java.util.Optional;

public class OptionalExample {

public static void main(String[] args) {

Optional<String> optional = Optional.of("Java");

System.out.println(optional.orElse("Default Value"));

Optional<String> emptyOptional = Optional.empty();

System.out.println(emptyOptional.orElse("Default Value"));

6. Explain the use of default methods in interfaces.

Java 8 introduced default methods that allow you to add method implementations directly in
interfaces. This is useful when you want to add new methods to an existing interface without

breaking the implementing classes.

Example:

interface MyInterface {

default void greet() {

System.out.println("Hello from default method");

public class DefaultMethodExample implements MyInterface {

public static void main(String[] args) {

DefaultMethodExample obj = new DefaultMethodExample();

obj.greet();

7. What is the difference between map and flatMap in Java 8 streams?

map: Transforms each element individually.

flatMap: Flattens the stream of collections into a single stream of elements.

Example:

import java.util.*;

public class MapVsFlatMap {

public static void main(String[] args) {

List<List<Integer>> listOfLists = Arrays.asList(


Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5, 6)

);

listOfLists.stream()

.map(list -> list.stream())

.forEach(System.out::println); // map

listOfLists.stream()

.flatMap(list -> list.stream())

.forEach(System.out::println); // flatMap

8. What is method reference in Java 8?

Method references are a shorthand syntax for calling a method, often used in place of a lambda

expression when the method is already defined.

Example:

import java.util.*;

public class MethodReferenceExample {

public static void main(String[] args) {

List<String> names = Arrays.asList("John", "Jane", "Jack");

names.forEach(System.out::println); // Method reference

9. What is the reduce method in Java 8 streams?


The reduce method is a terminal operation that performs a reduction on the elements of the stream

using an associative accumulation function and returns an Optional of the result.

Example:

import java.util.*;

public class ReduceExample {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4);

int sum = numbers.stream().reduce(0, (a, b) -> a + b);

System.out.println("Sum: " + sum);

10. What is the forEach method in Java 8 streams?

The forEach method is a terminal operation that iterates over the elements of the stream and

performs the provided action for each element.

Example:

import java.util.*;

public class ForEachExample {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

numbers.stream().forEach(System.out::println);

}
11. What is the difference between anyMatch, allMatch, and noneMatch in Java 8 streams?

anyMatch: Returns true if any element matches the given predicate.

allMatch: Returns true if all elements match the given predicate.

noneMatch: Returns true if no elements match the given predicate.

Example:

import java.util.*;

public class MatchExample {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

System.out.println(numbers.stream().anyMatch(n -> n > 3)); // true

System.out.println(numbers.stream().allMatch(n -> n > 3)); // false

System.out.println(numbers.stream().noneMatch(n -> n > 6)); // true

12. How does collect work in streams?

The collect method is a terminal operation that transforms the stream's elements into a different

form, such as a list, set, or map.

Example:

import java.util.*;

import java.util.stream.*;

public class CollectExample {

public static void main(String[] args) {


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

List<Integer> evenNumbers = numbers.stream()

.filter(n -> n % 2 == 0)

.collect(Collectors.toList());

System.out.println(evenNumbers);

13. What is the purpose of the peek method in Java 8 streams?

The peek method allows you to perform an action on each element of the stream without modifying

the stream. It is mainly used for debugging purposes.

Example:

import java.util.*;

public class PeekExample {

public static void main(String[] args) {

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

numbers.stream()

.peek(n -> System.out.println("Processing: " + n))

.filter(n -> n % 2 == 0)

.forEach(System.out::println);

14. What is the Optional class, and how do you use it?

Optional is a container object that may or may not contain a non-null value. It provides methods like
isPresent(), ifPresent(), and orElse() to handle null values in a cleaner way.

Example:

import java.util.Optional;

public class OptionalExample {

public static void main(String[] args) {

Optional<String> name = Optional.of("Madhu");

name.ifPresent(n -> System.out.println("Hello " + n));

Optional<String> emptyName = Optional.empty();

System.out.println(emptyName.orElse("No name present"));

15. Explain the new Date and Time API in Java 8.

Java 8 introduced the java.time package, which includes classes like LocalDate, LocalTime,

LocalDateTime, ZonedDateTime, and more to handle dates and times more efficiently.

Example:

import java.time.*;

public class DateTimeExample {

public static void main(String[] args) {

LocalDate today = LocalDate.now();

System.out.println(today);

You might also like