フリー問題

Java SE 21 Developer Professional のフリー問題 7 / 20 問目

問題文

A method must map each value of an enum with twelve constants to a different string. Which characteristic makes a switch expression preferable to a chain of if and else if statements here?

選択肢

  1. A switch expression converts the enum value to an int automatically, so the comparison is done on numbers instead of on object references and cannot fail.
  2. A switch expression evaluates all of its arms in one pass, which makes it faster than a chain of separate tests.
  3. The compiler checks that every enum constant is covered and reports an error when one is missing and no default arm is present.
  4. A switch expression allows an arm to fall through into the next arm, which lets several enum constants share one result without repeating the code.

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

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