Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What method is implied to be added by the compiler to all enum classes?

  1. values()

  2. valueOf()

  3. toString()

  4. None of the above

The correct answer is: values()

The compiler adds the method values() to all enum classes by default. This method is used to create an array of all the enum constants defined inside the enum class. Option B, valueOf(), is incorrect because this method is used to return the enum constant value associated with the specified string if the values match. Option C, toString(), is incorrect because this method returns the string representation of the enum constant. Option D, None of the above, is incorrect because the compiler does add a method to enum classes, it just happens to be values(). So the correct answer is A, values().