Java 8 Objective Questions

1. Which of the following feature is introduced in Java 8?

a) Serialization
b) Stream API
c) Abstraction
d) Polymorphism

Check Answer

2. Which of the following is not a core concept of Java 8’s new date and time API?

a) ZonedTime
b) LocalDate
c) LocalDateTime
d) ZonedDateTime

Check Answer

3. Which of these is a new method added to the Collection interface in Java 8?

a) forEach()
b) iterator()
c) size()
d) get()

Check Answer

4. What does the Optional class provide in Java 8?

a) Provides a container that may or may not contain a non-null value
b) Provides additional utility methods for arrays
c) Provides a way to handle unchecked exceptions
d) None of the above

Check Answer

5. Which of the following is a functional interface in Java 8?

a) Comparator
b) Runnable
c) ActionListener
d) All of the above

Check Answer

6. What does the lambda expression (String s) -> s.length() return?

a) A function that takes a string and returns its length
b) A method that prints the length of a string
c) A string with its length appended
d) None of the above

Check Answer

7. Which method reference type is used for a static method?

a) ClassName::staticMethodName
b) objectName::instanceMethodName
c) ClassName::new
d) None of the above

Check Answer

8. Which package contains the classes for the new date and time API in Java 8?

a) java.util.time
b) java.time
c) java.date
d) java.util.date

Check Answer

9. What is the main benefit of the Stream API in Java 8?

a) To allow parallel processing of collections
b) To enhance security
c) To reduce the size of the code
d) None of the above

Check Answer

10. Which method is used to filter elements of a stream in Java 8?

a) filter()
b) select()
c) choose()
d) pick()

Check Answer

11. Which of the following interfaces is not a functional interface?

a) Predicate
b) Supplier
c) Comparator
d) Serializable

Check Answer

12. Which of the following is a method reference to an instance method of a particular object?

a) ClassName::instanceMethodName
b) objectName::instanceMethodName
c) ClassName::new
d) None of the above

Check Answer

13. How do you obtain a stream from a collection in Java 8?

a) collection.getStream()
b) collection.stream()
c) collection.obtainStream()
d) collection.createStream()

Check Answer

14. Which of these is used to represent a value that might be absent?

a) Stream
b) Optional
c) Supplier
d) Consumer

Check Answer

15. What is the purpose of the default keyword in an interface?

a) To declare a constant
b) To provide a default implementation for a method
c) To specify the access level
d) None of the above

Check Answer

16. Which of these new methods in Java 8 can be used to get the current date?

a) LocalDate.now()
b) Date.today()
c) CurrentDate.now()
d) LocalDate.today()

Check Answer

17. Which method is used to create an infinite stream?

a) Stream.infinite()
b) Stream.generate()
c) Stream.iterate()
d) Both b and c

Check Answer

18. Which of these is a new collector added in Java 8?

a) Collectors.toList()
b) Collectors.toSet()
c) Collectors.toMap()
d) All of the above

Check Answer

19. Which method in Stream API is used to perform a reduction on the elements of the stream using an associative accumulation function?

a) reduce()
b) accumulate()
c) collect()
d) gather()

Check Answer

20. Which of the following functional interfaces has a method that accepts a single argument and produces a result?

a) Consumer
b) Supplier
c) Function
d) Predicate

Check Answer

21. Which of the following is not a type of method reference in Java 8?

a) Static method reference
b) Instance method reference of an arbitrary object
c) Constructor reference
d) Enum method reference

Check Answer

22. What is the use of the Stream.of() method?

a) To create a stream from an array or a list of values
b) To filter elements in a stream
c) To map elements in a stream
d) None of the above

Check Answer

23. Which of these methods is used to return the first element of a stream, if present?

a) findFirst()
b) getFirst()
c) first()
d) head()

Check Answer

24. Which of the following is a terminal operation in the Stream API?

a) map()
b) filter()
c) forEach()
d) peek()

Check Answer

25. What is the output of Optional.ofNullable(null).isPresent()?

a) true
b) false
c) Throws NullPointerException
d) None of the above

Check Answer

26. What is the purpose of the flatMap() method in the Stream API?

a) To flatten a stream of streams into a single stream
b) To map elements to a new stream
c) To filter elements based on a condition
d) None of the above

Check Answer

27. Which of these is not a new feature in Java 8?

a) Default methods in interfaces
b) Lambda expressions
c) Method references
d) Generics

Check Answer

28. What is the purpose of the Collectors.toMap() method?

a) To collect elements into a Map
b) To convert a List to a Map
c) To map elements from one stream to another
d) None of the above

Check Answer

29. Which of these methods is used to convert a stream to a list?

a) stream.toList()
b) collect(Collectors.toList())
c) toList()
d) stream.collectList()

Check Answer

30. What is the main purpose of the Supplier functional interface?

a) To consume a value
b) To provide a value
c) To map a value
d) To test a value

Check Answer

31. What does the peek() method do in the Stream API?

a) Returns a new stream that is a subset of the original stream
b) Performs an action for each element of the stream and returns the original stream
c) Removes elements that match a given predicate
d) None of the above

Check Answer

32. What is the purpose of the Predicate functional interface?

a) To map elements
b) To supply elements
c) To test elements
d) To consume elements

Check Answer

33. Which method of the Optional class returns the value if present, otherwise throws NoSuchElementException?

a) get()
b) orElse()
c) orElseThrow()
d) orElseGet()

Check Answer

34. How do you create an empty Optional object?

a) Optional.empty()
b) Optional.of()
c) Optional.ofNullable()
d) Optional.create()

Check Answer

35. Which method is used to convert a stream to an array?

a) stream.toArray()
b) toArray()
c) collectArray()
d) array()

Check Answer

36. What is the output of Stream.of(1, 2, 3).map(x -> x * 2).collect(Collectors.toList())?

a) [1, 2, 3]
b) [2, 4, 6]
c) [1, 4, 9]
d) [3, 6, 9]

Check Answer

37. Which method of the Stream API is used to sort the elements of a stream?

a) sorted()
b) order()
c) arrange()
d) sequence()

Check Answer

38. What is the purpose of the Function functional interface?

a) To map a value to another value
b) To provide a value
c) To test a value
d) To consume a value

Check Answer

39. What does the filter() method do in the Stream API?

a) Removes elements that match a given predicate
b) Converts a stream to a collection
c) Maps elements to another stream
d) None of the above

Check Answer

40. Which of these methods is used to obtain the count of elements in a stream?

a) count()
b) size()
c) length()
d) total()

Check Answer

41. Which method of the Stream API is used to concatenate two streams?

a) concat()
b) merge()
c) join()
d) combine()

Check Answer

42. Which functional interface represents an operation that accepts a single input argument and returns no result?

a) Function
b) Consumer
c) Supplier
d) Predicate

Check Answer

43. Which of these methods is used to reduce the elements of a stream to a single value?

a) reduce()
b) sum()
c) accumulate()
d) total()

Check Answer

44. What is the purpose of the Collectors.joining() method?

a) To concatenate the elements of a stream into a single string
b) To join two streams
c) To collect elements into a set
d) None of the above

Check Answer

45. Which method in the Stream API is used to remove duplicate elements?

a) distinct()
b) unique()
c) filter()
d) None of the above

Check Answer

46. Which of the following is a method of the Optional class to provide an alternative value if the original value is not present?

a) orElse()
b) ifPresent()
c) get()
d) isPresent()

Check Answer

47. What is the purpose of the Collectors.groupingBy() method?

a) To group the elements of a stream by a classifier function
b) To sort the elements of a stream
c) To filter the elements of a stream
d) None of the above

Check Answer

48. Which method in the Stream API is used to create a parallel stream?

a) parallel()
b) parallelStream()
c) parallelize()
d) parallelCollection()

Check Answer

49. Which of these methods is used to return an Optional describing the specified value, if non-null, otherwise returns an empty Optional?

a) ofNullable()
b) of()
c) empty()
d) Optional()

Check Answer

50. Which method is used to check if a value is present in an Optional?

a) isPresent()
b) exists()
c) hasValue()
d) getValue()

Check Answer