MCQs on Java 8 Features | Modern Java Features

MCQs on Java 8 Features

Lambda Expressions

  1. Which of the following is a correct lambda expression syntax?
    • a) (int x, int y) -> x + y
    • b) (x, y) -> {return x + y;}
    • c) (int x, int y) { x + y; }
    • d) x, y -> x + y
  2. In Java 8, lambda expressions provide support primarily for which type of programming?
    • a) Functional
    • b) Procedural
    • c) Object-Oriented
    • d) Imperative
  3. Which of these statements is true about lambda expressions in Java?
    • a) Lambda expressions can only be used in loops.
    • b) Lambdas are not compatible with functional interfaces.
    • c) Lambda expressions simplify syntax for anonymous functions.
    • d) Lambda expressions are used for method overloading.
  4. What is a functional interface in Java 8?
    • a) An interface with multiple abstract methods
    • b) An interface with a single abstract method
    • c) An interface with no methods
    • d) An interface with only default methods
  5. Which of the following is a common functional interface in Java 8?
    • a) Predicate
    • b) Loop
    • c) Controller
    • d) InterfaceHandler

Streams API

  1. Which statement best describes Java 8’s Streams API?
    • a) It processes data in a sequential manner only.
    • b) It is used for networking in Java.
    • c) It provides a way to process collections of objects in a functional style.
    • d) It replaces the file handling package.
  2. Which method would you use to filter elements in a Stream?
    • a) map
    • b) filter
    • c) reduce
    • d) forEach
  3. What does the map method do in the Streams API?
    • a) Filters elements
    • b) Applies a function to each element and returns a new stream
    • c) Reduces elements to a single value
    • d) Combines streams
  4. In Java Streams, which method is used to convert elements in a stream to a list?
    • a) collect(Collectors.toList())
    • b) toList()
    • c) stream()
    • d) filter()
  5. Which method is used to terminate a stream in Java 8?
    • a) map
    • b) filter
    • c) reduce
    • d) terminate

Default and Static Methods in Interfaces

  1. Which new method type did Java 8 introduce to interfaces?
    • a) Private
    • b) Default
    • c) Protected
    • d) Synchronized
  2. Default methods in interfaces were introduced in Java 8 for which purpose?
    • a) To allow interfaces to be backward compatible
    • b) To restrict method overriding
    • c) To enforce encapsulation
    • d) To prevent subclassing
  3. Can default methods be overridden by implementing classes?
    • a) Yes
    • b) No
    • c) Only in abstract classes
    • d) Only if marked final
  4. Which keyword is used to define a static method in an interface?
    • a) default
    • b) final
    • c) static
    • d) interface
  5. What is a key benefit of static methods in interfaces?
    • a) They can be inherited by implementing classes.
    • b) They provide utility functions related to the interface.
    • c) They enable method overloading in subclasses.
    • d) They enforce data encapsulation.

Method References

  1. Which of the following represents a method reference in Java 8?
    • a) ClassName::methodName
    • b) methodName.ClassName
    • c) methodName(ClassName)
    • d) ClassName-methodName
  2. Which method reference type refers to an instance method of a particular object?
    • a) ClassName::methodName
    • b) objectName::methodName
    • c) super::methodName
    • d) method::objectName
  3. How does System.out::println work in Java 8?
    • a) It’s a lambda expression to print text.
    • b) It references an instance method for printing.
    • c) It’s a method that creates a stream.
    • d) It initializes the System class.
  4. Which type of method reference is used to refer to a constructor?
    • a) ClassName::new
    • b) new ClassName()
    • c) ClassName.methodName
    • d) ::ClassName
  5. Method references can be seen as shorthand for:
    • a) Anonymous inner classes
    • b) Complex functions
    • c) Constructors only
    • d) Enumerations

Lambda Expressions and Streams API Combined

  1. Which statement is true about combining lambda expressions and streams in Java 8?
    • a) They are primarily used for creating interfaces.
    • b) They allow for functional-style processing of data collections.
    • c) They are incompatible with each other.
    • d) They can only process numeric data.
  2. What does the following lambda expression with streams do?
javaCopy codeList<String> names = Arrays.asList("Anna", "John", "Emma");
names.stream().filter(name -> name.startsWith("E")).collect(Collectors.toList());
sqlCopy code- a) Collects all names
- b) Collects names starting with "A"
- c) Collects names starting with "E"
- d) Collects only empty names

23. What does Optional in Java 8 help with? – a) Handling null values – b) Replacing arrays – c) Managing exceptions – d) Iterating over collections

  1. Which function would help you avoid null pointer exceptions in streams?
    • a) reduce
    • b) Optional
    • c) map
    • d) noneMatch
  2. Which method returns an Optional in Streams API?
    • a) filter
    • b) findFirst
    • c) reduce
    • d) map

Additional Questions on Java 8 Features

  1. Java 8 introduced which new package primarily for Date and Time API?
    • a) java.time
    • b) java.date
    • c) java.calendar
    • d) java.datetime
  2. Which feature was added to enhance concurrency in Java 8?
    • a) java.concurrent
    • b) CompletableFuture
    • c) ThreadLocal
    • d) ConcurrentModification
  3. Which is NOT a core Java 8 feature?
    • a) Streams API
    • b) Serialization
    • c) Lambda Expressions
    • d) Default Methods
  4. Collectors.toMap() in Streams API is used to:
    • a) Create a list from a stream
    • b) Filter elements in a stream
    • c) Convert a stream into a map
    • d) Sort elements in a stream
  5. What is the role of Collectors.groupingBy()?
    • a) Group elements based on a classifier function
    • b) Sort stream elements
    • c) Split streams into arrays
    • d) Create a single-value result

Answer Table

QnoAnswer
1a) (int x, int y) -> x + y
2a) Functional
3c) Lambda expressions simplify syntax for anonymous functions.
4b) An interface with a single abstract method
5a) Predicate
6c) It provides a way to process collections of objects in a functional style.
7b) filter
8b) Applies a function to each element and returns a new stream
9a) collect(Collectors.toList())
10c) reduce
11b) Default
12a) To allow interfaces to be backward compatible
13a) Yes
14c) static
15b) They provide utility functions related to the interface.
16a) ClassName::methodName
17b) objectName::methodName
18b) It references an instance method for printing.
19a) ClassName::new
20a) Anonymous inner classes
21b) They allow for functional-style processing of data collections.
22c) Collects names starting with “E”
23a) Handling null values
24b) Optional
25b) findFirst
26a) java.time
27b) CompletableFuture
28b) Serialization
29c) Convert a stream into a map
30a) Group elements based on a classifier function

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top