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?
-
Direction d = NORTH;
-
Nav.Direction d = NORTH;
-
Direction d = Direction.NORTH;
-
Nav.Direction d = Nav.Direction.NORTH;
The Direction enum is defined inside the Nav class (line 11), making it a nested enum type. To reference it from outside code (line 14 in Sprite class), you must use the full qualified name: outer class name, then inner enum name, then the enum constant. Option D shows correct syntax: 'Nav.Direction d = Nav.Direction.NORTH;' - Nav (outer class) contains Direction (enum), which contains the constant NORTH. Option A misses the Nav prefix, Option B misses Direction enum name, and Option C misses the Nav prefix entirely.
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.