フリー問題

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

問題文

A method opens a database connection, runs one query, and walks through the rows of the result. Which arrangement releases the three resources correctly?

選択肢

  1. Close the connection first and the result set last, because the outermost resource has to be released before the ones inside it
  2. Keep one connection in a static field and share it for the whole run of the program, because a connection is safe for use from several threads by contract
  3. Declare all three of them as ordinary variables and let the collector release them once the method returns, because the driver registers a hook for that
  4. Declare the connection, the statement, and the result set in one try-with-resources header, which closes all three of them in reverse order

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

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