Java SE 8 Programmer II Exam: Question 1Z0-809 Solution

Java SE 8 Programmer II Exam: Question 1Z0-809 Solution

Question

Given: public enum USCurrency{ PENNY (1), NICKLE(5), DIME (10), QUARTER(25); private int value; public USCurrency(int value){ this.value = value; } public int getValue() {return value;} } public class Coin { public static void main (String[] args){ USCurrency usCoin =new USCurrency.DIME; System.out.println(usCoin.getValue()): } } Which two modifications enable the given code to compile? (Choose two.)

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

BC.

The given code has several compilation errors, and we need to make two modifications to enable it to compile. Let's go through each answer choice to identify the changes that need to be made.

Option A: Nest the USCurrency enumeration declaration within the Coin class.

This option is incorrect because enum types must be declared in their own file or in a static context. Nesting the enum declaration inside the Coin class would result in a compilation error.

Option B: Make the USCurrency enumeration constructor public.

This option is incorrect because the USCurrency enum constructor is already defined as public. The constructor takes an integer value argument and assigns it to the private instance variable value.

Option C: Remove the new keyword from the instantiation of usCoin.

This option is correct. The instantiation of the usCoin variable is incorrect because it tries to create a new instance of an enum. Enums are predefined values, and we cannot create new instances of them. To fix the compilation error, we need to remove the "new" keyword from the instantiation of the usCoin variable. Instead, we can directly assign one of the predefined values of the USCurrency enum to the usCoin variable. For example, we can modify the line as follows:

USCurrency usCoin = USCurrency.DIME;

Option D: Make the getValue() method public.

This option is incorrect because the getValue() method is already defined as public. The method returns the value of the USCurrency enum value.

Option E: Add the final keyword in the declaration of value.

This option is incorrect because the addition of the final keyword in the declaration of the value instance variable does not affect the compilation of the code. It is a good practice to declare instance variables as final if their value does not change, but it is not necessary to fix the compilation errors in the given code.

Therefore, the correct answers are Option C and Option D. We need to remove the new keyword from the instantiation of the usCoin variable and ensure that the getValue() method is already defined as public.