Skip to content
Sharp

Modern Java · interview question

How can you iterate over all constants of an enum in Java?

Multiple choiceMediumchapter-7-curatedpremiummodern-javaenums
Choose one answer
Answer and explanation

Correct answer

D. Using the values() method

Why

The values() method returns an array containing all of the constants of the enum type, allowing you to easily iterate over them using a loop.

Test yourself on Modern Java

A free test with instant, explained results. No account needed.

Start free test
All Modern Java questions →