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…
Question
Options
- AMarketing
- BFinance
- CTechnical
- DUndefined
- EMarketing
How the community answered
(53 responses)- A6% (3)
- B2% (1)
- C79% (42)
- D11% (6)
- E2% (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 CTOonly fires if the switch receivesDesig.CTO;valueOf("CMO")never yields that. - B (Finance) - Same reasoning;
case CFOnever matches here. - D (Undefined) - The
defaultbranch is unreachable becauseDesig.CMOmatchescase CMOfirst. - 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
Community Discussion
No community discussion yet for this question.