🎙 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 4 of 10

What does this print? (code)

What does this print?

enum Coin {
    ONE(1), 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);
    }
}
  1. A.3
  2. B.0
  3. C.1510
  4. D.16
Answer & explanation

Correct answer: 16

Each constant stores its value: 1 + 5 + 10 = 16.

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 →