Exam 1z0-829 All QuestionsBrowse all questions from this exam
Question 7

Given:

What is the result?

    Correct Answer: C

    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'.

Discussion
minhdevOption: C

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

james2033Option: 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

UtemanOption: C

C is the correct answer

SampsOption: C

C is correct