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);
a) 15
b) 10
c) 5
d) 20
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);
a) 1
b) 2
c) 3
d) 0
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);
a) ALICE, BOB, CHARLIE
b) Alice, Bob, Charlie
c) ALICE BOB CHARLIE
d) Alice Bob Charlie
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);
a) 1
b) 2
c) 3
d) 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);
a) abc
b) abcd
c) abcde
d) a
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);
a) [h, e, l, o, w, r, d]
b) [hello, world]
c) [h, e, l, o, w, o, r, l, d]
d) [h, e, l, l, o, w, o, r, l, d]
7. What will the following Java code snippet output?
IntStream.range(1, 4).forEach(System.out::print);
a) 123
b) 1234
c) 234
d) 134
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);
a) 246
b) 135
c) 123456
d) 24
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);
a) true
b) false
c) Compilation error
d) Runtime exception
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);
a) apple
b) banana
c) cherry
d) date
Comments
Post a Comment
Leave Comment