Java Streams MCQs with answers Page - 6

You will find multiple-choice questions (MCQs) related to #Java Streams here. Go through these questions to prepare effectively for your upcoming exams and interviews.

To view the correct answer for any question, simply click the "Show Answer" button.

Have a question to share? Click on "Add Question" to contribute!

A

Admin • 831.35K Points
Coach

Q. What is the default behavior of `sorted()`?

  • (A) Sorts by hash code
  • (B) Sorts using natural order
  • (C) Sorts by toString()
  • (D) Sorts by insertion order

A

Admin • 831.35K Points
Coach

Q. How can you avoid a `NullPointerException` while using streams?

  • (A) Use empty lists only
  • (B) Use null-safe streams like `Optional.ofNullable()`
  • (C) Avoid using `stream()`
  • (D) Convert everything to primitive types

A

Admin • 831.35K Points
Coach

Q. What is the result of `Stream.of(1, 2, 3).reduce(1, (a, b) -> a * b)`?

  • (A) 6
  • (B) 12
  • (C) 0
  • (D) 7

A

Admin • 831.35K Points
Coach

Q. Which method will throw if there is no element in the stream?

  • (A) findFirst().orElse(null)
  • (B) findFirst().get()
  • (C) findAny().isPresent()
  • (D) findFirst().orElseThrow()

A

Admin • 831.35K Points
Coach

Q. What is the output type of `Collectors.toMap()`?

  • (A) List
  • (B) Set
  • (C) Map
  • (D) Queue

A

Admin • 831.35K Points
Coach

Q. Which stream operation is best suited to calculate the average of a list of numbers?

  • (A) reduce()
  • (B) collect(Collectors.summingInt())
  • (C) mapToInt().average()
  • (D) count()

A

Admin • 831.35K Points
Coach

Q. What does `Collectors.toCollection(TreeSet::new)` do?

  • (A) Returns a HashSet
  • (B) Returns a TreeSet containing sorted elements
  • (C) Returns a List
  • (D) Returns a Map

A

Admin • 831.35K Points
Coach

Q. What happens if duplicate keys are provided to `Collectors.toMap()`?

  • (A) Last one wins
  • (B) Exception is thrown
  • (C) Ignored
  • (D) Merged automatically
What's Tag

As you may know, questions are organized under broad categories. Each category can include various types of questions. For example, the "History" category might contain questions about the Revolt of 1857, Shivaji Maharaj, Ancient History, Buddhism, and more.

To further refine this organization, we've introduced tags, which act as sub-categories to group questions more specifically.

Verified users can add tags to any question. If you have any suggestions regarding this system, we'd love to hear from you. Contact Us

Learn More