Java 8 Quiz - MCQ - Multiple Choice Questions And Answers
Q. What is the correct way to iterate over a List in Java 8 using a lambda expression?
A. list.forEach((element) -> { ... });B. for (element : list) { ... };
C. list.stream().forEach((element) -> { ... });
D. for (int i = 0; i < list.size(); i++) { ... };
Q. What is the purpose of default methods in interfaces in Java 8?
A. To provide multiple inheritance supportB. To force implementation in implementing classes
C. To override methods from Object class
D. To add new methods to interfaces without breaking existing implementations
Q. What is the output of the following Java 8 stream operation?
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Dave");
names.stream().filter(name -> name.length() > 4).forEach(System.out::println);
A. AliceB. Charlie
C. Dave
D. Bob
Q. What is the purpose of the Supplier functional interface in Java 8?
A. To consume valuesB. To transform values
C. To produce values
D. To filter values
Q. Which Java 8 interface is a functional interface that represents a function that accepts two arguments and produces a result?
A. ConsumerB. Function
C. BiFunction
D. Supplier
Q. What is the purpose of the Collectors.groupingBy() method in Java 8?
A. To filter elements based on a predicate conditionB. To partition elements into two groups based on a predicate condition
C. To group elements by a classification function
D. To sort elements based on a comparator
Q. What is a lambda expression in Java 8?
A. An anonymous methodB. A named method
C. A static method
D. An abstract method
Q. Which interface in Java 8 identifies a single method operation that takes a single input argument and produces a result?
A. RunnableB. Producer
C. Function
D. Consumer
Q. What is the purpose of the Predicate functional interface in Java 8?
A. To produce resultsB. To consume values
C. To filter elements based on a condition
D. To transform values