1z0-829 Exam QuestionsBrowse all questions from this exam

1z0-829 Exam - Question 7


Given:

What is the result?

Show Answer
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

4 comments
Sign in to comment
james2033Option: C
Feb 11, 2024

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

minhdevOption: C
May 5, 2024

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

SampsOption: C
Feb 10, 2024

C is correct

UtemanOption: C
Jul 2, 2024

C is the correct answer