Java Streams API Interview Questions

    Question 1JAVA - Streams Basics

    What is the main advantage of using Streams in Java?

    Question 2JAVA - Stream Creation

    Which of the following is a correct way to create a stream from a list?

    Question 3JAVA - Filter Operation

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); nums.stream().filter(n -> n % 2 == 0).forEach(System.out::print); } }

    Question 4JAVA - Map Operation

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> words = Arrays.asList("a", "bb", "ccc"); words.stream().map(String::length).forEach(System.out::print); } }

    Question 5JAVA - Reduce Operation

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4); int sum = nums.stream().reduce(0, (a, b) -> a + b); System.out.println(sum); } }

    Question 6JAVA - Collectors

    Which method collects stream results into a list?

    Question 7JAVA - Stream Count

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> names = Arrays.asList("John", "Jane", "Jack"); long count = names.stream().filter(s -> s.startsWith("J")).count(); System.out.println(count); } }

    Question 8JAVA - Parallel Stream

    Which method is used to create a parallel stream from a list?

    Question 9JAVA - Distinct & Sorted

    What will this code print?

    import java.util.*; public class Test { public static void main(String[] args) { List<Integer> nums = Arrays.asList(5, 1, 2, 2, 3); nums.stream().distinct().sorted().forEach(System.out::print); } }

    Question 10JAVA - AnyMatch Example

    What will this code return?

    import java.util.*; public class Test { public static void main(String[] args) { List<String> list = Arrays.asList("apple", "banana", "cherry"); boolean result = list.stream().anyMatch(s -> s.contains("a")); System.out.println(result); } }