フリー問題

Java SE 17 Developer のフリー問題 5 / 20 問目

問題文

A method turns an enum value into a display label. The team wants compilation to fail later if somebody adds a new constant to the enum and forgets to give it a label. Which construct provides that guarantee?

選択肢

  1. A switch expression with a default arm that yields an empty label, because a default arm makes the compiler check the arms more strictly
  2. A switch statement with a default arm that raises an exception, because the statement form also has to list every constant before it will compile
  3. A chain of if statements that compares the value with equals, because the compiler checks that such a chain ends with an else branch
  4. A switch expression with one arm for each constant of the enum and no default arm, because such an expression has to be exhaustive at compile time

解答・解説を確認するには

正解と解説の確認、回答の記録には無料登録が必要です。登録すると演習モードでフリー問題に回答し、正誤と解説をその場で確認できます。