Enum Switch Result with Unused Stream dropWhile

Answer Correct answer: A — The switch statement matches the CMO enum constant and prints Marketing, while the unused dropWhile stream operation is ignored.

Given: and the code fragment: What is the result? - image - image

  1. Marketing Correct Answer
  2. Marketing -
  3. Marketing -
  4. UnDefined

Community Votes

A
100%

100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The question tests understanding of intermediate stream operations versus terminal operations, and how a switch statement matches an enum constant.

This question tests the interaction between an unused Stream dropWhile operation and a switch expression on an enum. The page establishes that the intermediate stream operation is discarded and the switch correctly matches the enum value.

Choosing an option based on the dropWhile operation modifying the enum, failing to realize that dropWhile is an intermediate operation whose result is unused.

Community Discussion (4 comments)

SrinivasJasti 👍 1 Selected: A
The result of the dropWhile operation is unused because the method call is not assigned to a variable or consumed.
xplorerpj 👍 1 Selected: A
A is correct answer
james2033 👍 3 Selected: A
package q33; import java.util.Arrays; public class Q33 { public static void main(String[] args) { Arrays.stream(Desig.values()).dropWhile(s -> s.equals(Desig.CMO)); switch (Desig.valueOf("CMO")) { case CEO -> System.out.println("Executive"); case CMO -> System.out.println("Marketing"); case CFO -> System.out.println("Finance"); case CTO -> System.out.println("Technical"); default -> System.out.println("UnDefined"); } } } // Result: // Marketing
supersquax 👍 1
A is correct, tested in online compiler.

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The Arrays.stream(Desig.values).dropWhile(...) call is an intermediate operation on a stream. Because its result is not assigned to a variable or consumed by a terminal operation, it has no effect on the program state. The switch statement evaluates Desig.valueOf("CMO"), which resolves to the CMO enum constant. This matches case CMO, executing System.out.println("Marketing") and printing Marketing.

Why the Other Options Are Wrong

Options B and C suggest that the output includes a trailing dash or additional text, which does not align with the println("Marketing") statement in the code. Option D, UnDefined, would only be printed if the switch fell through to the default case, which doesn't happen because valueOf("CMO") successfully matches case CMO.

Community Comment Notes

Commenters noted that the dropWhile operation is unused. As SrinivasJasti pointed out, "dropWhile operation is unused because the method call is not assigned to a variable or consumed." James2033 provided the reconstructed code showing the switch block, confirming that the stream call is a no-op and the switch directly matches the CMO constant.

Official Reference

Exam Strategy

Always check if stream operations are terminal or intermediate. An intermediate operation without a terminal operation or variable assignment does absolutely nothing to the underlying data or program flow.

Related Analysis

← Back to 1Z0-829 Study Guide