Learn. Practice. Feel prepared.
Java 8 Objective Questions and Answers - Set 2
This Java 8 quiz is designed to test your skills with modern Java features. It focuses on practical understanding, output-based questions, and advanced concepts commonly asked in interviews and real-world development. This is part 2 of the series.
Practice Java 8 MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
16 What is the main benefit of parallel streams? Medium
Correct answer:
Utilization of multiple CPU cores
Parallel streams split tasks across multiple cores, though performance gains are workload-dependent.
17 Which method converts a Stream back to a List? Medium
Correct answer:
collect(Collectors.toList())
Collectors.toList() is used with collect to transform a stream into a List.
18 What does the peek() method do in a stream? Medium
Correct answer:
Performs debugging or side effects
peek is mainly used for debugging or observing elements as they flow through the stream.
19 Which interface is the base for all lambda expressions? Medium
Correct answer:
No specific base interface
Lambda expressions can target any functional interface; there is no single base interface.
20 What will be the output? Optional<String> opt = Optional.ofNullable(null); System.out.println(opt.orElse("Java")); Medium
Correct answer:
Java
orElse returns the default value when the Optional is empty.
21 Which Java 8 feature enables behavior parameterization? Medium
Correct answer:
Lambdas
Lambdas allow passing behavior as parameters to methods.
22 Which Collector is used to group elements in a stream? Medium
Correct answer:
groupingBy()
groupingBy collects elements into a Map based on a classifier function.
23 What does CompletableFuture improve over Future? High
Correct answer:
Asynchronous composition
CompletableFuture supports non-blocking, asynchronous composition of tasks.
24 Which method was added to String in Java 11? Medium
Correct answer:
isBlank()
isBlank checks whether a string is empty or contains only whitespace.
25 Which statement about streams is correct? Medium
Correct answer:
Streams are lazy
Streams are lazy and only process data when a terminal operation is invoked.
26 Which functional interface returns a boolean result? Medium
Correct answer:
Predicate
Predicate represents a function that evaluates a condition and returns true or false.
27 What is the default behavior of Comparator.comparing()? Medium
Correct answer:
Natural ascending order
Comparator.comparing sorts elements in natural ascending order by default.
28 Which Java feature allows interfaces to have method implementations? Medium
Correct answer:
Default methods
Default methods in interfaces allow providing method implementations.
29 What will happen if two interfaces provide the same default method? High
Correct answer:
Compilation error unless overridden
The implementing class must override the method to resolve the conflict.
30 Which method creates an infinite stream? Medium
Correct answer:
Stream.generate()
Stream.generate produces an infinite stream unless limited.