Java

Java Java 8 Interview Questions

42 questions with answers · Java Interview Guide

Lambda expressions, functional interfaces, Optional, method references, and the Date/Time API introduced in Java 8.

bar_chartQuick stats
Total questions42
High frequency3
With code examples3
1

What is Stream in Java?

Stream provides lazy, functional pipeline for processing collections; chains operations (map, filter, reduce) without modifying source, enabling parallel processing.

java
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
nums.stream().filter(n -> n > 2).map(n -> n * 2).forEach(System.out::println);
2

What is a functional interface

Functional interface has exactly one abstract method, allowing lambda expression implementation; annotated with @FunctionalInterface (e.g., Comparator, Function).

java
@FunctionalInterface interface Calculator {
  int calculate(int a, int b);
}
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(5, 3));
3

What is Lambda

Lambda is a concise anonymous function syntax (parameter -> expression) in Java 8+ that enables functional programming, often used with functional interfaces and streams.

java
List<String> fruits = Arrays.asList("apple", "banana", "orange");
fruits.forEach(f -> System.out.println(f));
Function<Integer, Integer> square = x -> x * x;
4

Have you heard something about the Foreach cycle

Enhanced for-loop (for-each) iterates over collections and arrays using Iterator internally; syntax: for(Type item : collection) eliminates index management and is cleaner than traditional for loops.

5

What mechanism is used in the implementation of parallel streams

ForkJoinPool implements parallel streams using work-stealing algorithm where idle threads steal tasks from busy threads' queues, enabling efficient multi-core processing.

6

What is a link to the method and how it is realized

Method references are shortcuts for lambda expressions that call existing methods using syntax ClassName::methodName; internally compiled to invokedynamic bytecode instruction for runtime dispatch.

7

What innovations appeared in Java 8 and JDK 8

Java 8 introduced lambdas, streams API, functional interfaces, method references, default methods in interfaces, java.time package, and the Optional class.

8

What variables have access to lambda-expression

Lambda expressions can access effectively final local variables and all instance/static variables from the enclosing scope.

9

How to sort the list of lines using lambda expression

Use `Collections.sort(list, (s1, s2) -> s1.compareTo(s2))` or `list.sort((s1, s2) -> s1.compareTo(s2))` for sorting with lambda expressions.

10

What types of links to methods do you know

Four types: static method references (Class::staticMethod), instance method references (obj::method), constructor references (Class::new), and type method references (String::compareTo).

11

Explain the expression system.out :: println

System.out::println is a method reference that passes System.out.println as a Consumer functional interface, equivalent to x -> System.out.println(x).

12

Why are the functional interfaces of `Function <T, R>, DOUBLEFUNCTION <L>, IntFUNCTION <L>, LONGFUNCTION <R>` `` ``

Primitive functional interfaces (DoubleFunction, IntFunction, LongFunction) avoid autoboxing overhead when working exclusively with primitive types.

13

Why are the functional interfaces of `Binaryoparator <t>, DoubleBinaryoprator, Intbinaryoprator, Longbinaryoprator`

Primitive BinaryOperator interfaces avoid autoboxing and are optimized for primitive arithmetic operations returning the same primitive type.

14

Why are the functional interfaces `Predicate <t>, DoublePredicate, IntPredicate, Longpredicate`

Primitive Predicate interfaces avoid boxing and provide optimized predicates for primitive type boolean tests.

15

Why are the functional interfaces of `consumer <t>, DoubleConsumer, Intconsumer, Longconsumer`

Primitive Consumer interfaces avoid boxing for operations that consume primitive values without returning a result.

16

Why do you need a functional interface `biconsumer <t ,u>`

BiConsumer<T, U> accepts two parameters of different types and performs an action without returning a result.

17

Why do you need a functional interface `bifunction <t, u, r>`

BiFunction<T, U, R> accepts two parameters of different types and returns a result of type R, useful for binary operations.

18

Why do you need a functional interface `bipredicate <t ,u>`

BiPredicate<T, U> accepts two parameters of different types and returns a boolean, for testing conditions on two values.

19

Why are functional interfaces of the type `_to_FUNCTION`

Primitive conversion functional interfaces (ToDoubleFunction, ToIntFunction, ToLongFunction) convert from a type T to a primitive type.

20

Why are the functional interfaces of `TODOBLEBIFUNCTION <T, U>, tointBifunction <T, U>, Tolongbifunction <T, U>`

ToDoubleBiFunction, ToIntBiFunction, ToLongBiFunction convert from two types T and U to their respective primitive types.

21

What are the functional interfaces of `TODOBLEFUNCTION <T>, tointFUNction <T>, TOLONGFUNCTION <T>` `

ToDoubleFunction<T>, ToIntFunction<T>, ToLongFunction<T> convert from type T to double, int, and long primitives respectively.

22

What are the functional interfaces of `Objdobleconsumer <t>, objintconsumer <t>, objlongconsumer <t>` `

ObjDoubleConsumer, ObjIntConsumer, ObjLongConsumer consume an object and a primitive type without returning a result.

23

What is Stringjoiner

StringJoiner efficiently concatenates string sequences with a delimiter, prefix, and suffix, replacing manual StringBuilder concatenation.

24

How to call the Default interface method in the class implementing this interface

Call a default method using `super.methodName()` in the implementing class, or explicitly reference it as `InterfaceName.super.methodName()`.

25

What is a Static interface method

Static interface methods are concrete methods in interfaces (introduced Java 8) that can be called via the interface name without an instance, similar to static class methods.

26

How to call a static interface method

Call static interface methods using the interface name directly: InterfaceName.staticMethod(), not through implementing classes or instances.

27

What are the ways to create stream

Streams can be created from Collections (collection.stream()), Arrays (Arrays.stream()), primitive sources (IntStream.range()), or using Stream.of() and Stream.generate().

28

What is the difference between Collection and Stream

Collections are in-memory data structures that hold all elements eagerly; Streams are lazy, functional pipelines that process elements on-demand without storing them.

29

What is the Collect () method in straps for

collect() is a terminal operation that transforms stream elements into a different collection type using Collectors (e.g., toList(), toMap(), groupingBy()).

30

Why are the MAP () and Maptoint (), MaptodOble (), Maptolong () are intended in streams.

map() transforms each element to another value, while mapToInt/Double/Long() specialize in primitive streams to avoid boxing overhead and enable primitive-specific operations.

31

Why in straps the LIMIT () method is intended

limit(n) is an intermediate operation that truncates the stream to return only the first n elements, useful for pagination or preventing infinite streams.

32

Why is the Sorted () method intended in streams

sorted() is an intermediate operation that orders stream elements either naturally or via a custom Comparator before passing them downstream.

33

Why are the Flatmap (), Flatmaptoint (), FlatmaptodOble (), Flatmaptolong () methods for straps are intended for streams.

flatMap() flattens nested streams by mapping each element to a stream and merging results; primitive variants (flatMapToInt/Double/Long) return primitive streams efficiently.

34

What final methods of working with streams do you know

Terminal operations: forEach(), collect(), reduce(), min(), max(), count(), anyMatch(), allMatch(), findFirst(), findAny() - these produce final results or side effects.

35

What intermediate methods of working with streams do you know

Intermediate operations: filter(), map(), flatMap(), distinct(), sorted(), peek(), limit(), skip() - these return streams and can be chained.

36

What additional methods for working with associative arrays (Maps) appeared in Java 8

Java 8 Map methods: getOrDefault(), putIfAbsent(), merge(), compute(), computeIfPresent(), computeIfAbsent(), forEach() for iteration without entrySet().

37

What is LocalDetetime

LocalDateTime represents an immutable date-time without timezone (e.g., 2024-01-15T10:30:00), used for local operations independent of geographic location.

38

What is ZoneDDATETEME

ZonedDateTime represents an immutable date-time with timezone information (e.g., 2024-01-15T10:30:00+02:00[Europe/Paris]), handling zone-aware calculations.

39

What is NASHORN

Nashorn is a JavaScript engine that allows running JavaScript code on the JVM, included in Java 8-14; enables Java-JavaScript interoperability.

40

What is JJS

jjs is a command-line tool (Java Shell) that executes JavaScript files directly using the Nashorn engine without needing a separate JavaScript runtime.

41

What class appeared in Java 8 for coding/decoding data

Base64 class appears in java.util package for encoding/decoding Base64 data via Base64.getEncoder() and Base64.getDecoder().

42

Why are the functional interfaces of `Unaryoprator, Doubleunaryoprator, IntunaryOperator, Longunaryoprator`

UnaryOperator and its primitive variants (DoubleUnaryOperator, IntUnaryOperator, LongUnaryOperator) are functional interfaces that take a single argument and return a result of the same type, avoiding boxing overhead for primitives.

Knowing the answers is half the battle

The other half is explaining them clearly under pressure.

Try a free mock interviewarrow_forward

More Java topics