nerdexam
Oracle

1Z0-829 · Question #16

public enum Desig { CEO('A'), CMO('B'), CTO('C'), CFO('D'); char c; private Desig(char c) { this.c = c; } } and the code fragment: Arrays.stream(Desig.values()).dropWhile(s -> s.equals(Desig.CMO))…

The correct answer is C. Technical. There is an error in the stated answer key - the code actually prints "Marketing", making A or E correct, not C. Here's why: Desig.valueOf("CMO") performs a case-sensitive lookup of the enum constant named "CMO", which returns Desig.CMO with no exception. The switch then…

Working with Streams and Lambda expressions

Question

public enum Desig { CEO('A'), CMO('B'), CTO('C'), CFO('D'); char c; private Desig(char c) { this.c = c; } } and the code fragment: 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"); } What is the result?

Options

  • AMarketing
  • BFinance
  • CTechnical
  • DUndefined
  • EMarketing

How the community answered

(53 responses)
  • A
    6% (3)
  • B
    2% (1)
  • C
    79% (42)
  • D
    11% (6)
  • E
    2% (1)

Explanation

There is an error in the stated answer key - the code actually prints "Marketing", making A or E correct, not C.

Here's why:

Desig.valueOf("CMO") performs a case-sensitive lookup of the enum constant named "CMO", which returns Desig.CMO with no exception. The switch then matches case CMO and prints "Marketing". The Arrays.stream(...).dropWhile(...) line on the preceding line is a complete red herring - it has no terminal operation (no .collect(), .forEach(), .toList(), etc.), so the stream is created but never evaluated; it has zero effect on program state.

Why the distractors fail:

  • C (Technical) - case CTO only fires if the switch receives Desig.CTO; valueOf("CMO") never yields that.
  • B (Finance) - Same reasoning; case CFO never matches here.
  • D (Undefined) - The default branch is unreachable because Desig.CMO matches case CMO first.
  • A and E (both "Marketing") - Having two identical options is itself a distractor designed to make students doubt the obvious answer; the correct output is indeed "Marketing".

Memory tip: When you see a stream chain with no terminal operation, mentally delete that entire line - streams are lazy and do absolutely nothing without a terminal operation. Then focus on what remains.

Note to exam takers: The answer key marking C as correct appears to be a mistake in this question. If you encounter this on an exam, "Marketing" (whichever letter it appears under) is the defensible answer.

Topics

#enums#pattern matching#streams#switch expressions

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice