Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 67

Given this enum declaration:

Examine this code:

System.out.println(Alphabet.getFirstLetter());

What code should be written at line 3 to make this code print A?

    Correct Answer: B

    To make the code print 'A', the getFirstLetter method must return the string representation of the constant A from the enum. Option B correctly uses the toString() method on A and declares getFirstLetter as static, ensuring it can be called on the enum type Alphabet without an instance. Thus, the proper code should be: static String getFirstLetter() { return A.toString(); }. This ensures the method returns 'A' as required by the question.

Discussion
d7bb0b2Option: B

A-> Missing } ,and if this is fixed, Alphabet.values()[1] => print B B-> ok C- > instance method so => Alphabet.getFirsletter() throw compilation error D-> same as C C AND D only works in an element of enum: Alphabet.A.getFirstLetter() not in static context Alphabeg.getFirstletter()

d7bb0b2Option: A

TWO OPTIONS ARE OK: A AND B; C AND D , are invalid because ther aren't static so cannot call with Alphabet.method().

d7bb0b2

Sorry only b, CAUSE A [1] must be [0]

d7bb0b2

and A missing }

OmnisumemOption: B

Tested: B.

StavokOption: B

B is correct

tmuralimanoharOption: B

Answer:B

Mukes877Option: B

B is correct Because Static is for not creating any object and values()[1] gives -> B

ObaltOption: B

Correct answer is B