🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Java Enums: all 10 questions

Quiz: Java Enums · Question 5 of 10

What does this print? (code)

What does this print?

enum Coin {
    ONE(1), TWO(2), FIVE(5), TEN(10);
    final int value;
    Coin(int value) { this.value = value; }
}

public class Main {
    public static void main(String[] args) {
        int total = 0;
        for (Coin c : Coin.values()) total += c.value;
        System.out.println(total + " " + Coin.FIVE.ordinal());
    }
}
  1. A.18 3
  2. B.4 2
  3. C.18 5
  4. D.18 2
Answer & explanation

Correct answer: 18 2

1 + 2 + 5 + 10 = 18, and FIVE is at position 2.

Can you get the rest right without peeking?

Take the timed Quiz: Java Enums quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →