1Z0-809 practice question 20 of 25
Given: public enum USCurrency { PENNY (1), NICKLE(5), DIME (10), QUARTER(25); private int value; public USCurrency(int value) { this.value = value; }…
Full set: 196 questionsThere are two compilation errors in the provided code. First, enum constructors cannot be declared public or protected; they are implicitly private. The public modifier on the USCurrency constructor must be removed or changed to private. Second, enum constants are static, final instances and are not created using the new keyword. The line new USCurrency.DIME is incorrect syntax for accessing an enum constant. It must be changed to USCurrency.DIME. Therefore, making the constructor private and removing the new keyword will allow the code to compile.
A. Nesting the enum is a stylistic choice and is not required for the code to compile. A top-level enum is perfectly valid.
D. The getValue() method is an instance method that correctly retrieves the value for a specific enum constant (e.g., DIME). Making it static would be incorrect.
E. While making the value field final is good practice, it is not required for the code to compile.
1. Oracle, The Java™ Language Specification, Java SE 8 Edition. Section 8.9, "Enums". It states, "An enum constant is implicitly static and final."
2. Oracle, The Java™ Language Specification, Java SE 8 Edition. Section 8.9.2, "Enum Body and Member Declarations". It specifies, "It is a compile-time error if the constructor of an enum type is declared public or protected."
3. Oracle, The Java™ Tutorials. "Enum Types". docs.oracle.com/javase/tutorial/java/javaOO/enum.html. This tutorial shows the correct syntax for declaring and using enums, including accessing constants without the new keyword.
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 2Given: class Student { String course, name, city; public Student (String name, String cou…
- Question 3Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { …
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
