Search Tutorials


Java 8 Quiz - MCQ - Multiple Choice Questions And Answers | JavaInUse

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 support
B. 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. Alice
B. Charlie
C. Dave
D. Bob

Q. What is the purpose of the Supplier functional interface in Java 8?

A. To consume values
B. 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. Consumer
B. 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 condition
B. 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 method
B. 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. Runnable
B. Producer
C. Function
D. Consumer

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values





Q. Which Java 8 feature simplifies the process of iterating, filtering, and processing collections?

A. Streams
B. Lambdas
C. Optional
D. Default methods

Q. Which of the following code snippets correctly demonstrates the use of lambda expressions in Java 8?

A.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(number -> System.out.println(number));
B.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach((String name) -> {
    System.out.println("Hello, " + name);
});
C.
List<Double> prices = Arrays.asList(10.5, 20.0, 30.75);
prices.forEach((Double price) -> System.out.println("Price: $" + price));
D.
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
ages.forEach((name, age) -> System.out.println(name + " is " + age + " years old"));

Q. How can you sort a List of Strings in Java 8 in alphabetical order?

A.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
names.stream()
    .sorted()
    .forEach(System.out::println);
B.
List<String> names = Arrays.asList("David", "Charlie", "Bob", "Alice");
names.sort();
names.forEach(System.out::println);
C.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
Collections.sort(names);
names.forEach(System.out::println);
D.
List<String> names = Arrays.asList("David", "Charlie", "Bob", "Alice");
names.stream()
    .sorted(Comparator.reverseOrder())
    .forEach(System.out::println);

Q. What does the functional interface Consumer in Java 8 represent?

A.
It represents a function that accepts one argument and produces a result.
B.
It represents a function that accepts two arguments and produces a result.
C.
It represents a function that accepts an argument and returns no result.
D.
It represents a function that accepts no arguments and produces a result.

Q. How can you convert an ArrayList to a Stream in Java 8?

A.
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Stream<Integer> stream = numbers.stream();
B.
ArrayList<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie"));
Stream<String> stream = names.toStream();
C.
ArrayList<Double> prices = new ArrayList<>(Arrays.asList(10.5, 20.0, 30.75));
Stream<Double> stream = Stream.of(prices);
D.
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
Stream<Integer> stream = numbers.getStream();

Q. What does the Optional class in Java 8 represent?

A.
It represents a container for an optional value that may or may not be present.
B.
It represents a set of utility methods for working with collections.
C.
It represents a class for handling exceptions in Java programs.
D.
It represents a class for creating custom annotations in Java programs.

Q. What is the purpose of the Stream API in Java 8?

A.
It is a framework for defining collections in Java programs.
B.
It is a mechanism for handling file I/O operations in Java programs.
C.
It is a library for working with date and time values in Java programs.
D.
It is a set of classes and interfaces for processing sequences of elements in Java programs.

Q. In Java 8, which interface is used to define custom functional interfaces?

A.
Runnable
B.
Function
C.
Consumer
D.
@FunctionalInterface

Q. How can you filter elements in a Stream using a predicate in Java 8?

A.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
    .filter(number -> number % 2 == 0)
    .forEach(System.out::println);
B.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.stream()
    .filter(name -> name.startsWith("A"))
    .forEach(System.out::println);
C.
List<Double> prices = Arrays.asList(10.5, 20.0, 30.75);
prices.stream()
    .filter(price -> price < 20.0)
    .forEach(System.out::println);
D.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
    .filter(number >= 3)
    .forEach(System.out::println);

Q. Which of the following code snippets demonstrates the correct configuration of a Spring Boot REST Controller?

A.
@RestController
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
B.
@Controller
@RequestMapping("/api")
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
C.
@RestController
@RequestMapping("/api")
public class MyController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
D.
@Controller
public class MyController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        return "Hello, World!";
    }

Q. What is the purpose of the Collectors class in Java 8?

A.
It is used to define custom collectors for streams in Java programming.
B.
It is used to perform reductions on streams and return a single result.
C.
It is used to transform the elements of a stream into a different data structure.
D.
It is used to partition the elements of a stream based on a predicate.