🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Welcome to the Java Streams API Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of the Java Streams API. Each question has a correct and brief explanation.
1. What is the output of the following Java code snippet?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println(sum);
Answer:
Explanation:
The reduce method performs a reduction on the elements of the stream with the provided identity value and accumulator function, which in this case sums up the numbers.
2. What does this Java code snippet output?
Stream<String> stream = Stream.of("Java", "Python", "C++");
long count = stream.filter(s -> s.startsWith("J")).count();
System.out.println(count);
Answer:
Explanation:
The filter method filters the stream based on the given predicate. Only "Java" starts with "J", so the count is 1.
3. Identify the output of the following code:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
String result = names.stream().map(String::toUpperCase).collect(Collectors.joining(", "));
System.out.println(result);
Answer:
Explanation:
The map method converts each element to upper case, and collect with Collectors.joining concatenates them with ", " as the delimiter.
4. What will be printed by this Java code?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> max = numbers.stream().max(Integer::compare);
max.ifPresent(System.out::println);
Answer:
Explanation:
The max method finds the maximum element based on the given comparator. The maximum value in the list is 5.
5. What does this code snippet output?
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
list.stream().limit(3).forEach(System.out::print);
Answer:
Explanation:
The limit method limits the stream to the first 3 elements, so it prints "a", "b", and "c".
6. What is the result of executing this code?
List<String> words = Arrays.asList("hello", "world");
List<String> uniqueLetters = words.stream()
.flatMap(word -> Arrays.stream(word.split("")))
.distinct()
.collect(Collectors.toList());
System.out.println(uniqueLetters);
Answer:
Explanation:
flatMap breaks each word into individual letters, distinct removes duplicates, and collect gathers the result into a list.
7. What will the following Java code snippet output?
IntStream.range(1, 4).forEach(System.out::print);
Answer:
Explanation:
IntStream.range(1, 4) generates a sequential stream from 1 (inclusive) to 4 (exclusive), so it prints 1, 2, 3.
8. What does the following code snippet print?
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::print);
Answer:
Explanation:
The filter method filters elements that are even, and forEach prints each remaining element.
9. Determine the output of this Java code:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
boolean allMatch = names.stream().allMatch(name -> name.length() > 3);
System.out.println(allMatch);
Answer:
Explanation:
The allMatch method checks whether all elements in the stream match the given predicate. Here, all names have more than 3 characters, so it returns true.
10. What is the result of the following code snippet?
List<String> fruits = Arrays.asList("apple", "banana", "cherry", "date");
String result = fruits.stream()
.filter(fruit -> fruit.contains("a"))
.sorted()
.findFirst()
.orElse("None");
System.out.println(result);
Answer:
Explanation:
The stream is filtered to include only fruits containing "a", sorted in a natural order, and findFirst gets the first element in the sorted stream. "apple" is the first alphabetically.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment