class Nav{ 11. public enum Direction { NORTH, SOUTH, EAST, WEST } 12. } 13. public class Sprite{ 14. // insert code here 15. } Which code, inserted at line 14, allows the Sprite class to compile?

  1. Direction d = NORTH;

  2. Nav.Direction d = NORTH;

  3. Direction d = Direction.NORTH;

  4. Nav.Direction d = Nav.Direction.NORTH;


Correct Option: D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Direction d = NORTH; - This option is incorrect because the enum values are accessed using the enum name. In this case, the enum name is "Nav.Direction", so the correct syntax would be "Nav.Direction.NORTH".

Option B) Nav.Direction d = NORTH; - This option is incorrect because the enum values are accessed using the enum name, not just the enum constant. In this case, the correct syntax would be "Nav.Direction.NORTH".

Option C) Direction d = Direction.NORTH; - This option is incorrect because the enum values are accessed using the enum name. In this case, the enum name is "Nav.Direction", so the correct syntax would be "Nav.Direction.NORTH".

Option D) Nav.Direction d = Nav.Direction.NORTH; - This option is correct because it correctly accesses the enum constant "NORTH" from the enum "Direction" within the class "Nav". This allows the Sprite class to compile.

The correct answer is D. This option is correct because it uses the correct syntax to access the enum constant within the specified enum class.

Find more quizzes: