Java MCQs – Java 8 Features

21.) In a class that implements multiple interfaces, how is ambiguity resolved if two interfaces provide default methods with the same name?

A) The class must implement both methods explicitly.
B) The compiler throws an error, and the class must choose one method to implement.
C) The class inherits the default method from the first interface it implements.
D) The class inherits the default method from the most specific interface it implements.

Answer: Option D

Explanation: In case of method name conflicts, the class inherits the method from the most specific interface.

22.) What is the primary advantage of the Nashorn JavaScript engine over the older Rhino engine?

A) Improved performance
B) Better support for Java code integration
C) Simpler syntax for JavaScript
D) Enhanced debugging capabilities

Answer: Option A

Explanation: The Nashorn engine offers improved JavaScript performance.

23.) Which method in the CompletableFuture class is used to apply a transformation function to the result?

A) thenAccept()
B) thenApply()
C) thenCombine()
D) thenRun()

Answer: Option B

Explanation: The thenApply() method applies a transformation function to the result.

24.) What is a potential drawback of using parallel streams in Java?

A) Increased complexity of code
B) Limited support for multi-core processors
C) Inefficient use of CPU cores
D) Risk of thread-related issues

Answer: Option D

Explanation: Using parallel streams can introduce thread-related issues.

25.) Which collector is used to group elements based on a classification function?

A) groupingBy()
B) partitioningBy()
C) mapping()
D) collectingAndThen()

Answer: Option A

Explanation: The groupingBy() collector is used to group elements based on a classification function.

26.) In method references, what does :: denote?

A) Method invocation
B) Method definition
C) Variable assignment
D) Null reference

Answer: Option A

Explanation: In method references, :: denotes method invocation.

27.) Which class is used to represent a period of time with both date and time components?

A) LocalDate
B) LocalTime
C) LocalDateTime
D) Period

Answer: Option C

Explanation: LocalDateTime represents a period with both date and time components.

28.) Can a class override a default method inherited from an interface?

A) Yes, using the override keyword
B) Yes, by providing a new implementation
C) No, it’s not allowed
D) No, default methods are final

Answer: Option B

Explanation: A class can provide a new implementation for an inherited default method.

29.) What is the purpose of the distinct() intermediate operation in the Streams API?

A) It removes duplicate elements from a stream.
B) It sorts the elements in a stream.
C) It filters elements based on a predicate.
D) It performs a reduction operation.

Answer: Option A

Explanation: The distinct() operation removes duplicate elements from a stream.

30.) Which functional interface represents a function that takes two arguments and returns a result?

A) Function
B) BiFunction
C) Consumer
D) Predicate

Answer: Option B

Explanation: BiFunction represents a function that takes two arguments and returns a result.

Leave a Reply

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