Java Enum toString vs valueOf Output

Answer Correct answer: C — The program prints the ordinal of SUNNY (0) followed by the result of the overridden toString() method (SNOWY).

Given: What is the result? - image

  1. 0 CLOUDY
  2. 1 SNOWY
  3. 0 SNOWY Correct Answer
  4. 1 RAINY
  5. Compilation fails

Community Votes

C
100%

100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.

Community Insight

The exam tests knowledge of enum internals: ordinal() gives the index (0), while printing an enum calls its toString(), which can be overridden to return any string, such as 'SNOWY'.

This question tests the behavior of Java enums, specifically how ordinal() and overridden toString() methods interact with valueOf(). The correct result is determined by understanding that valueOf returns the enum constant, which triggers the custom toString method.

Learners often choose D or A because they assume the enum prints its name (RAINY or CLOUDY) instead of recognizing that the toString() method has been explicitly overridden to return 'SNOWY'.

Community Discussion (4 comments)

Uteman 👍 1
C is the correct answer
minhdev 👍 2
C is correct answer. Explanation: ordinal() -> get the index of enum_value - 0 valueOf(CLOUDY) = CLOUDY, but it prints a enum -> toString is called -> SNOWY
james2033 👍 3 Selected: C
package q07; public class Weather { public enum Forecast { SUNNY, CLOUDY, RAINY; @Override public String toString() { return "SNOWY"; } } public static void main(String[] args) { System.out.print(Forecast.SUNNY.ordinal() + " "); System.out.print(Forecast.valueOf("cloudy".toUpperCase())); } } // Result: // 0 SNOWY
Samps 👍 1 Selected: C
C is correct

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The code defines an enum Forecast with constants SUNNY, CLOUDY, and RAINy. The ordinal method returns the zero-based index of the enum constant, so Forecast.SUNNY.ordinal outputs 0. The second part uses Forecast.valueOf("cloudy".toUpperCase), which resolves to Forecast.CLOUDY. When this enum constant is passed to System.out.print, it implicitly calls the toString method. Since toString is overridden to return "SNOWY", the output is 0 SNOWY.

Why the Other Options Are Wrong

Options A, B, and D are incorrect because they fail to account for the overridden toString method. Option A assumes the name is printed but gets the ordinal wrong or confuses the constant. Option B suggests a different ordinal or name. Option D suggests the name is printed without override. Option E is incorrect because the code is syntactically valid Java.

Community Comment Notes

The community consensus strongly supports answer C. As user james2033 noted, the code results in 0 SNOWY due to the override. User minhdev explained that ordinal gets the index and valueOf returns the constant, triggering the custom toString.

Exam Strategy

Always check if an enum has an overridden toString() method when printing enum values. If not overridden, it prints the constant name; if overridden, it prints whatever the method returns.

Frequently Asked Questions

Why does valueOf return CLOUDY but print SNOWY?

valueOf returns the enum constant object Forecast.CLOUDY. When printed, Java calls the object's toString() method, which is overridden to return 'SNOWY'.

Is ordinal always 0 for the first enum?

Yes, ordinal() returns the index of the enum constant in the declaration order. SUNNY is the first constant, so its ordinal is 0.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide