This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Java 8 features”.
1. Which of the following is not introduced with Java 8?
a) Stream API
d) Lambda Expression
Explanation: Serialization is not introduced with Java 8. It was introduced with an earlier version of Java.
2. What is the purpose of BooleanSupplier function interface?
a) represents supplier of Boolean-valued results
b) returns Boolean-valued result
c) There is no such function interface
d) returns null if Boolean is passed as argument
Explanation: BooleanSupplier function interface represents supplier of Boolean-valued results.
3. What is the return type of lambda expression?
Explanation: Lambda expression enables us to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button.
4. Which is the new method introduced in java 8 to iterate over a collection?
a) for (String i : StringList)
b) foreach (String i : StringList)
Explanation: Traversing through forEach method of Iterable with anonymous class.
public void accept(Integer t)
//Traversing with Consumer interface implementation
MyConsumer action = new MyConsumer();
5. What are the two types of Streams offered by java 8?
a) sequential and parallel
b) sequential and random
c) parallel and random
d) random and synchronized
Explanation: Sequential stream and parallel stream are two types of stream provided by java.
Stream<Integer> sequentialStream = myList.stream();
Stream<Integer> parallelStream = myList.parallelStream();
6. Which feature of java 8 enables us to create a work stealing thread pool using all available processors at its target?
Explanation: Executors newWorkStealingPool() method to create a work-stealing thread pool using all available processors as its target parallelism level.
7. What does Files.lines(Path path) do?
a) It reads all the files at the path specified as a String
b) It reads all the lines from a file as a Stream
c) It reads the filenames at the path specified
d) It counts the number of lines for files at the path specified
Explanation: Files.lines(Path path) that reads all lines from a file as a Stream.
8. What is Optional object used for?
a) Optional is used for optional runtime argument
b) Optional is used for optional spring profile
c) Optional is used to represent null with absent value
d) Optional means it’s not mandatory for method to return object
Explanation: Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
Explanation: Nashorn provides 2 to 10 times faster in terms of performance, as it directly compiles the code in memory and passes the bytecode to JVM. Nashorn uses invoke dynamic feature.
10. What does SAM stand for in the context of Functional Interface?
a) Single Ambivalue Method
b) Single Abstract Method
c) Simple Active Markup
d) Simple Abstract Markup
Explanation: SAM Interface stands for Single Abstract Method Interface. Functional Interface is also known as SAM Interface because it contains only one abstract method.
Sanfoundry Global Education & Learning Series – Java Programming Language.