Java MCQs – Java 8 Features

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

A) They guarantee thread safety.
B) They allow for concurrent modification of collections.
C) They improve performance by utilizing multiple cores.
D) They provide a simpler syntax for stream operations.

Answer: Option C

Explanation: Parallel streams improve performance by utilizing multiple CPU cores.

12.) Which class provides utility methods for common collection operations, such as grouping and summarizing?

A) CollectionUtils
B) Collections
C) Collectors
D) CollectionOperations

Answer: Option C

Explanation: The Collectors class provides utility methods for common collection operations.

13.) What is the primary purpose of type annotations in Java 8?

A) To add metadata to classes and methods
B) To specify access modifiers
C) To define default implementations
D) To improve code readability

Answer: Option A

Explanation: Type annotations are used to add metadata to classes and methods.

14.) In Java 8, what replaced the PermGen space for class metadata storage?

A) Heap space
B) Metaspace
C) Stack space
D) Code cache

Answer: Option B

Explanation: Metaspace replaced PermGen for class metadata storage.

15.) Which garbage collector was introduced in Java 8 with a focus on improved performance?

A) Serial Garbage Collector
B) Parallel Garbage Collector
C) G1 Garbage Collector
D) CMS Garbage Collector

Answer: Option C

Explanation: Java 8 introduced the G1 Garbage Collector for improved performance.

16.) Which terminal operation in the Streams API returns the minimum element based on a provided comparator?

A) max()
B) reduce()
C) collect()
D) min()

Answer: Option D

Explanation: The min() terminal operation returns the minimum element.

17.) What class represents a specific instant in time, considering time zones?

A) Instant
B) ZonedDateTime
C) LocalDateTime
D) OffsetDateTime

Answer: Option B

Explanation: ZonedDateTime represents a specific instant in time with time zones.

18.) Which intermediate operation in the Streams API is used to filter elements based on a given predicate?

A) map()
B) reduce()
C) filter()
D) distinct()

Answer: Option C

Explanation: The filter() intermediate operation is used to filter elements based on a predicate.

19.) Which method in the Optional class returns the value if present, or a default value if not present?

A) ifPresent()
B) orElse()
C) orElseGet()
D) orElseThrow()

Answer: Option B

Explanation: The orElse() method returns the value if present, or a default value if not.

20.) Which class represents a specific point in time that can be used for scheduling tasks?

A) Instant
B) Duration
C) LocalTime
D) ScheduledTime

Answer: Option A

Explanation: Instant represents a specific point in time.

Leave a Reply

Your email address will not be published. Required fields are marked *