LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Predict the Output · Question 16

EnumSet ranges and complements Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 16 Back to Exercise

What is the output of the following Java program?

Main.java
import java.util.EnumSet;

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }

public class Main {
    public static void main(String[] args) {
        EnumSet<Day> work = EnumSet.range(Day.MON, Day.FRI);
        EnumSet<Day> weekend = EnumSet.complementOf(work);
        EnumSet<Day> gym = EnumSet.of(Day.SUN, Day.TUE, Day.THU);
        EnumSet<Day> gymOnWorkdays = EnumSet.copyOf(gym);
        gymOnWorkdays.retainAll(work);
        System.out.println(weekend + " " + gym + " " + gymOnWorkdays + " " + work.size());
    }
}
  • A [SAT, SUN] [TUE, THU, SUN] [TUE, THU] 5 Correct
  • B [SAT, SUN] [SUN, TUE, THU] [TUE, THU] 5
  • C [SUN, SAT] [TUE, THU, SUN] [TUE, THU] 5
  • D [SAT, SUN] [TUE, THU, SUN] [TUE, THU, SUN] 5
Correct Answer: Option A

EnumSet always keeps declaration order, whatever order the constants were added in.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links