Java 8 Quiz - MCQ - Multiple Choice Questions

This post contains a few useful Java 8 features multiple-choice questions to self-test your knowledge on Java 8 features such as Java 8 Lambda ExpressionsJava 8 Functional InterfacesJava 8 Method ReferencesJava 8 Stream API, etc.

The answer and explanation have been given for each MCQ.

1. Which of the following are valid lambda expressions? 

A.

String a, String b -> System.out.print(a+ b);

B.

() -> return;

C.

(int i) -> i;

D.

(int i) -> i++; return i;

Answer

The correct answer is C.

Explanation 

Option C is valid. The body doesn't need to use the return keyword if it only has one statement.

2. Given below code snippet

interface A {
     int aMethod(String s);
}
Which of the following are valid statements?

A.

A a = a -> a.length();

B.

A x = y -> {return y;};

C.

A s = "2" -> Integer.parseInt(s);

D.

A b = (String s) -> 1;

Answer

The correct answer is D.

Explanation

Option D is valid because it takes a String argument and returns an int value.

3. A lambda expression can be used...

A. As a method argument

B. As a conditional expression in an if statement

C. In a return statement

D. In a throw statement

Answer

The correct answers are A and C.

Explanation

Lambda expressions can be used in:
  • A variable declaration
  • An assignment
  • A return statement
  • An array initializer
  • As a method or constructor arguments
  • A ternary conditional expression
  • A cast expression

4. Which of the following are functional interfaces? (Select ALL that apply)

A. java.util.stream.Stream 

B. java.util.function.Consumer 

C. java.util.function.Supplier 

D. java.util.function.Predicate 

E. java.util.function.Function

Answer

B, C, D, and E

Explanation 

The interface java.util.stream.Stream is not a functional interface–it has numerous abstract methods. The other four options are functional interfaces. 

The functional interface java.util.function.Consumer has an abstract method with the signature void accept(T t).

The functional interface java.util.function.Supplier has an abstract method with the signature T get().

The functional interface java.util.function.Predicate has an abstract method with the signature boolean test(T t); 

The functional interface java.util.function.Function has an abstract method with the signature R apply(T t).

5. Choose the correct option based on this program:

import java.util.function.BiFunction;
public class StringCompare {
    public static void main(String args[]) {
        BiFunction < String, String, Boolean > compareString = (x, y) - >
            x.equals(y);
        System.out.println(compareString.apply("Java8", "Java8")); // #1
    }
}
A. This program results in a compiler error in the line marked with #1 

B. This program prints: true 

C. This program prints: false 

D. This program prints: (x, y) -> x.equals(y) 

E. This program prints: ("Java8", "Java8") -> "Java8".equals("Java8")

Answer

B. This program prints: true

Explanation 

The BiFunction interface takes two type arguments–they are of types String in this program. The return type is Boolean. 

The BiFunction functional interface has an abstract method named apply(). Since the signature of String’s equals() method matches that of the abstract method's signature, this program compiles fine. When executed, the strings “Java8” and “Java8” are equal; hence, the evaluation returns true that is printed on the console.

6. Which one of the following abstract methods does not take any argument but returns a value?

A. The accept() method in java.util.function.Consumer interface 

B. The get() method in java.util.function.Supplier interface 

C. The test() method in java.util.function.Predicate interface 

D. The apply() method in java.util.function.Function interface

Answer

B. The get() method in java.util.function.Supplier interface

Explanation 

The signature of get() method in java.util.function.Supplier interface is: T get().

7. Which of the following interfaces of the Java API can be considered functional?

A. java.util.concurrent.Callable

B. java.util.Map

C. java.util.Iterator

D. java.lang.Comparable

Answer

The correct answers are A and D.

Explanation 

java.util.concurrent.Callable is marked with the @FunctionalInterface annotation.

Although java.lang.Comparable is not marked with the @FunctionalInterface annotation, it can be considered one. Remember, this annotation doesn't make an interface functional.

8. Which of the following are intermediate operations?

A. limit

B. peek

C. anyMatch

D. skip

Answer

The correct answers are A, B, and D.

Explanation 

limitpeek, and skip are intermediate operations. The anyMatch is a terminal operation.

9. Which of the following are terminal operations?

A. sorted

B. flatMap

C. max

D. distinct

Answer

The correct answer is C.

Explanation 

max is the only terminal operation. sortedflatMap, and distinct are intermediate operations.

10. Which of the following are short-circuit operations?

A. reduce

B. parallel

C. findNone

D. findFirst

Answer

The correct answer is D.

Explanation 

findFirst is the only short-circuit operation. reduce is a terminal operation. the parallel is an intermediate operation. findNone doesn't exist.

11. Which of the following are valid ways to create a LocalDate object?

A.
LocalDate.of(2014);

B.

LocalDate.with(2014, 1, 30);

C.

LocalDate.of(2014, 0, 30);

D.

LocalDate.now().plusDays(5);

Answer

The correct answer is D.

Explanation

Option D is valid. The methods now() and plusDays() are valid and can be chained.

12. Which of the following are valid ChronoUnit values for LocalTime?

A. YEAR

B. NANOS

C. DAY

D. HALF_DAYS

Answer

The correct answers are B and D.

Explanation

LocalTime doesn't store information about years and (complete) days.

13. Which one of the following classes is best suited for storing timestamp values of application events in a file? 

A. java.time.ZoneId class 

B. java.time.ZoneOffset class 

C. java.time.Instant class 

D. java.time.Duration class 

E. java.time.Period class

Answer

C. Instant class

Explanation

The Instant class stores the number of seconds elapsed since the start of the Unix epoch (1970-01-01T00:00:00Z). 

The Instant class is suitable for storing a log of application events in a file as timestamp values. 

The ZoneId and ZoneOffset classes are related to time zones and hence are unrelated to storing timestamp values. 

The Duration class is for time-based values in terms of quantity of time (such as seconds, minutes, and hours). 

The Period class is for date-based values such as years, months, and days

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. JDBC Quiz
  11. Java Lambda Expressions Quiz
  12. Java Functional Interfaces Quiz
  13. Java Streams API Quiz
  14. Java Date Time Quiz
  15. Java 8 Quiz

Comments