This set of Java Multiple Choice Questions & Answers (MCQs) on “Java 8 features”.
1. Which of the following is not introduced with Java 8? Answer: b 2. What is the purpose of BooleanSupplier function interface? Answer: a 3. What is the return type of lambda expression? Answer: d 4. Which is the new method introduced in java 8 to iterate over a collection? 5. What are the two types of Streams offered by java 8? 6. Which feature of java 8 enables us to create a work stealing thread pool using all available processors at its target? Answer: b 7. What does Files.lines(Path path) do? Answer: b 8. What is Optional object used for? Answer: c 9. What is the substitute of Rhino javascript engine in Java 8? Answer: a 10. What does SAM stand for in the context of Functional Interface? Answer: b
a) Stream API
b) Serialization
c) Spliterator
d) Lambda Expression
Clarification: Serialization is not introduced with Java 8. It was introduced with an earlier version of Java.
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
Clarification: BooleanSupplier function interface represents supplier of Boolean-valued results.
a) String
b) Object
c) void
d) Function
Clarification: 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.
a) for (String i : StringList)
b) foreach (String i : StringList)
c) StringList.forEach()
d) List.for()
Clarification: Traversing through forEach method of Iterable with anonymous class.
StringList.forEach(new Consumer<Integer>()
{
public void accept(Integer t)
{
}
});
//Traversing with Consumer interface implementation
MyConsumer action = new MyConsumer();
StringList.forEach(action);
}
}
a) sequential and parallel
b) sequential and random
c) parallel and random
d) random and synchronized
Clarification: Sequential stream and parallel stream are two types of stream provided by java.
Stream<Integer> sequentialStream = myList.stream();
Stream<Integer> parallelStream = myList.parallelStream();
a) workPool
b) newWorkStealingPool
c) threadPool
d) workThreadPool
Clarification: Executors newWorkStealingPool() method to create a work-stealing thread pool using all available processors as its target parallelism level.
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
Clarification: Files.lines(Path path) that reads all lines from a file as a Stream.
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
Clarification: 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.
a) Nashorn
b) V8
c) Inscript
d) Narcissus
Clarification: 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.
a) Single Ambivalue Method
b) Single Abstract Method
c) Simple Active Markup
d) Simple Abstract Markup
Clarification: SAM Interface stands for Single Abstract Method Interface. Functional Interface is also known as SAM Interface because it contains only one abstract method.
