Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Easy
Question 45
Consider the code of Test.java file:
- package com.udayan.ocp;
- public class Test {
- enum Directions {
- NORTH("N"), SOUTH("S"), EAST("E"), WEST("W")
- private String notation;
- Directions(String notation) {
- this.notation = notation;
- }
- public String getNotation() {
- return notation;
- }
- }
- public static void main(String[] args) {
- System.out.println(Test.Directions.NORTH.getNotation());
- }
- }
What will be the result of compiling and executing Test class?
- A N
- B NORTH
- C Exception is thrown at runtime
- D Compilation error
Reveal correct answer
Correct answer: D
Explanation
As enum Directions contains more code after constant declarations, hence last constant declaration must be followed by a semicolon.
Correct constant declaration is:
NORTH("N"), SOUTH("S"), EAST("E"), WEST("W");
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
