Java 8 Quiz - MCQ - Multiple Choice Questions

Java 8 introduced several new features and enhancements to the programming language. In this blog post, we present a Java 8 quiz comprising 20+ multiple-choice questions. This quiz aims to assess your understanding of the new features and concepts introduced in Java 8, such as Java 8 Lambda ExpressionsJava 8 Functional InterfacesJava 8 Method ReferencesJava 8 Stream API, etc. Let's put your Java 8 knowledge to the test!

Learn and Master Java Programming: Learn Java Programming with Examples

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

1. What is a lambda expression in Java 8? 

a) A lightweight thread 
b) A lightweight process 
c) A concise way to represent an anonymous function 
d) A type of loop construct

Answer:

c) A concise way to represent an anonymous function 

Explanation: 

A lambda expression in Java 8 is a concise way to represent an anonymous function, allowing you to write more compact code. 

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

a) To perform I/O operations on files and directories 
b) To enable concurrent programming 
c) To manipulate collections of data in a functional style 
d) To handle exceptions in a functional programming paradigm 

Answer:

c) To manipulate collections of data in a functional style

Explanation: 

The Stream API in Java 8 provides a functional programming-style approach to manipulate collections of data, enabling operations like filtering, mapping, and reducing. 

3. Which annotation is used to declare a functional interface in Java 8? 

a) @FunctionalInterface 
b) @Interface 
c) @Lambda 
d) @Functional 

Answer:

a) @FunctionalInterface 

Explanation: 

The @FunctionalInterface annotation is used to declare a functional interface in Java 8, which is an interface with a single abstract method. 

4. How many types of method references are there in Java 8?

a) 2
b) 3 
c) 4
d) 5

Answer:

c) 4

Explanation: 

There are four types of method references in Java 8: static method references, instance method references of a particular object, instance method references of an arbitrary object, and constructor references.

5. What is the default method in an interface in Java 8? 

a) A method that can be overridden by implementing classes 
b) A method that is automatically inherited by implementing classes 
c) A method that cannot be overridden by implementing classes 
d) A method that can be accessed without an instance of the interface 

Answer:

b) A method that is automatically inherited by implementing classes

Explanation: 

A default method in an interface is automatically inherited by implementing classes and can be overridden if needed. 

6. What is the purpose of the Optional class in Java 8? 

a) To handle exceptions in a functional programming paradigm 
b) To represent nullable values safely and avoid NullPointerExceptions 
c) To perform I/O operations on files and directories 
d) To enable concurrent programming 

Answer:

b) To represent nullable values safely and avoid NullPointerExceptions 

Explanation: 

The Optional class in Java 8 is used to represent nullable values safely and prevent NullPointerExceptions by providing a way to handle empty or null values. 

7. What is the main advantage of using parallel streams in Java 8? 

a) Faster execution of sequential operations 
b) Improved memory utilization 
c) Automatic handling of exceptions 
d) Concurrent execution on multiple threads 

Answer:

d) Concurrent execution on multiple threads 

Explanation: 

Using parallel streams in Java 8 allows for concurrent execution of operations on multiple threads, potentially speeding up processing for large datasets. 

8. Which package contains the Predicate functional interface in Java 8? 

a) java.util 
b) java.lang 
c) java.util.function 
d) java.util.stream 

Answer:

c) java.util.function

Explanation: 

The Predicate functional interface, which represents a predicate (a boolean-valued function), is located in the java.util.function package in Java 8. 

9. What is the purpose of the forEach method in the Stream API in Java 8? 

a) To apply a function to each element of a stream 
b) To sort the elements of a stream 
c) To filter the elements of a stream based on a condition 
d) To combine the elements of a stream into a single result 

Answer:

a) To apply a function to each element of a stream 

Explanation: 

The forEach method in the Stream API is used to apply a function to each element of a stream, typically for performing an action on each element.

10. 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:

(int i) -> i;

Explanation:

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

11. 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:

D.

A b = (String s) -> 1;

Explanation:

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

12. 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

13. 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).

14. 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.

15. 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().

16. 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.

17. 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.

18. Which of the following are terminal operations?

A. sorted
B. flatMap
C. max
D. distinct

Answer

C. max

Explanation 

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

19. 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.

20. 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.

21. 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.

22. 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

23. What does the :: operator represent in Java 8? 

a) Ternary Operator 
b) Null Coalescing Operator 
c) Method Reference Operator 
d) Concatenation Operator 

Answer: 

c) Method Reference Operator 

Explanation: 

The :: is the method reference operator in Java 8, it is used to refer to methods of functional interfaces.

24. What does the default keyword do in Java 8 interfaces? 

a) It defines the default implementation of a method in an interface
b) It defines a default constructor
c) It specifies that a method should have a default visibility
d) It specifies that a method should be abstract

Answer:

a) It defines the default implementation of a method in an interface

Explanation:

In Java 8, the default keyword allows you to include non-abstract method declarations with implementations in interfaces.

25. What does the Java 8 feature "Method Reference" refer to? 

a) A shorthand syntax for writing lambda expressions that call existing methods 
b) A new way to create object references 
c) A reference to the memory address of a method 
d) A new type of method declaration 

Answer: 

a) A shorthand syntax for writing lambda expressions that call existing methods 

Explanation: 

Method references provide a way to refer to a method without invoking it, often used in the context of lambda expressions for improved readability and brevity.

Conclusion

Congratulations on completing the Java 8 quiz! We hope it challenged your knowledge and provided valuable insights into the new features and concepts introduced in Java 8. Understanding the enhancements in Java 8 is crucial for writing modern and efficient Java code. 

Learn everything about Java 8 features: Java 8 Tutorial and Examples

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills

Keep learning, practicing, and embracing the power of Java 8 in your programming journey!

Comments