Java 8 Objective Questions

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

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

Answer: b) Stream API

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

Answer: a) ZonedTime

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

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

Answer: a) forEach()

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

Answer: a) Provides a container that may or may not contain a non-null value

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

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

Answer: d) All of the above

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

Answer: a) A function that takes a string and returns its length

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

Answer: a) ClassName::staticMethodName

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

Answer: b) java.time

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

Answer: a) To allow parallel processing of collections

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

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

Answer: a) filter()

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

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

Answer: d) Serializable

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

Answer: b) objectName::instanceMethodName

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

Answer: b) collection.stream()

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

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

Answer: b) Optional

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

Answer: b) To provide a default implementation for a method

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

Answer: a) LocalDate.now()

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

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

Answer: d) Both b and c

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

Answer: d) All of the above

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

Answer: a) reduce()

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

Answer: c) Function

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

Answer: d) Enum method reference

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

Answer: a) To create a stream from an array or a list of values

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

Answer: a) findFirst()

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

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

Answer: c) forEach()

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

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

Answer: b) false

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

Answer: a) To flatten a stream of streams into a single stream

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

Answer: d) Generics

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

Answer: a) To collect elements into a Map

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

Answer: b) collect(Collectors.toList())

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

Answer: b) To provide a value

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

Answer: b) Performs an action for each element of the stream and returns the original stream

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

Answer: c) To test elements

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

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

Answer: a) get()

34. How do you create an empty Optional object?

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

Answer: a) Optional.empty()

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

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

Answer: b) toArray()

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]

Answer: b) [2, 4, 6]

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

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

Answer: a) sorted()

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

Answer: a) To map a value to another value

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

Answer: a) Removes elements that match a given predicate

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

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

Answer: a) count()

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

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

Answer: a) concat()

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

Answer: b) Consumer

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

Answer: a) reduce()

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

Answer: a) To concatenate the elements of a stream into a single string

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

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

Answer: a) distinct()

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

Answer: a) orElse()

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

Answer: a) To group the elements of a stream by a classifier function

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

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

Answer: b) parallelStream()

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

Answer: a) ofNullable()

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

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

Answer: a) isPresent()

What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now