Given:
What is the result?
Given:
What is the result?
The given code defines an enum 'Forecast' with three constants: SUNNY, CLOUDY, and RAINY. The toString method is overridden to return 'SNOWY' for any constant of this enum. In the main method, 'Forecast.SUNNY.ordinal()' is called, which returns the index of SUNNY, which is 0. Then 'Forecast.valueOf("cloudy".toUpperCase())' is evaluated, which converts 'cloudy' to 'CLOUDY' and retrieves the CLOUDY enum constant. Because the toString method is overridden, it returns 'SNOWY' when CLOUDY is printed. Therefore, the output is '0 SNOWY'.
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
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
C is the correct answer
C is correct