Given: 1. class Bigger { 2. public static void main(String[] args) { 3. // insert code here 4. } 5. } 6. class Better { 7. enum Faster {Higher, Longer}; 8. } Which, inserted independently at line 3, will compile? (Choose all that apply.)
-
Faster f = Faster.Higher;
-
Faster f = Better.Faster.Higher;
-
Better.Faster f = Better.Faster.Higher;
-
Bigger.Faster f = Bigger.Faster.Higher;
-
Better. Faster f2; f2 = Better.Faster.Longer;
-
Better b; b.Faster = f3; f3 = Better.Faster.Longer;
Faster is an enum defined inside class Better. To reference it outside, you need the fully qualified name Better.Faster. Options C and E use correct syntax: Better.Faster is the type, and values are Better.Faster.Higher and Better.Faster.Longer. Options A, B, D are missing the outer class name.
To answer this question, let's go through each option to understand whether it will compile or not:
Option A) Faster f = Faster.Higher;
This option will not compile because the enum Faster is defined inside the class Better, so it cannot be accessed directly without specifying the class name.
Option B) Faster f = Better.Faster.Higher;
This option will not compile because the enum Faster is defined inside the class Better, and it cannot be accessed directly without specifying the class name.
Option C) Better.Faster f = Better.Faster.Higher;
This option will compile because it correctly specifies the enum Faster and assigns one of its values (Higher) to the variable f.
Option D) Bigger.Faster f = Bigger.Faster.Higher;
This option will not compile because the enum Faster is not defined inside the class Bigger. It is defined inside the class Better.
Option E) Better. Faster f2; f2 = Better.Faster.Longer;
This option will compile because it declares a variable f2 of type Better.Faster and assigns one of its values (Longer) to f2 in a separate statement.
Option F) Better b; b.Faster = f3; f3 = Better.Faster.Longer;
This option will not compile because it tries to assign a value to a non-existent variable b.Faster. Additionally, the variable f3 is not declared before it is used.
The correct answers are C and E. These options will compile as they correctly access the enum Faster and assign its values to variables.