Q:

Java find output programs (Enumeration) | set 2

belongs to collection: Java find output programs

0

Find the output of Java programs | Enumeration | Set 2: Enhance the knowledge of Java Enumeration concepts by solving and finding the output of some Java programs.

Question 1:

public class EnumEx {
  enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week Vals[] = Week.values();

    for (Week w: Vals) {
      System.out.println(w + " at " + w.ordinal());
    }
  }
}

Question 2:

public class EnumEx {
  enum Week {
    Sun = 101,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week Vals[] = Week.values();

    for (Week w: Vals) {
      System.out.println(w + " at " + w.ordinal());
    }
  }
}

Question 3:

public class EnumEx {
  final enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week Vals[] = Week.values();
    int index = 0;
    for (Week w: Vals) {
      index = w.ordinal();
      System.out.println(w + " at " + index);
    }
  }
}

Question 4:

public class EnumEx {
  static enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week Vals[] = Week.values();
    int index = 0;
    for (Week w: Vals) {
      index = w.ordinal();
      System.out.println(w + " at " + index);
    }
  }
}

Question 5:

public class EnumEx {
  static enum Week {
    Sun,
    Mon,
    Tue,
    Wed,
    Thu,
    Fri,
    Sat
  }

  public static void main(String[] args) {
    Week Vals[] = Week.values();

    switch (Vals[3].ordinal()) {
    case 0:
      System.out.println("Sunday");
      break;
    case 1:
      System.out.println("Monday");
      break;
    case 2:
      System.out.println("Tuesday");
      break;
    case 3:
      System.out.println("Wednesday");
      break;
    case 4:
      System.out.println("Thrusday");
      break;
    case 5:
      System.out.println("Friday");
      break;
    case 6:
      System.out.println("Saturday");
      break;
    }
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

Sun at 0
Mon at 1
Tue at 2
Wed at 3
Thu at 4
Fri at 5
Sat at 6

Explanation:

In the above program, we created a class EnumEx that contains a user define enumeration Week and a main() method. The Week enumeration contains 7-week values.

In the main() method we created an array of Week enum, which is initialized with values of enum and then we accessed each value of Week enum using a foreach loop and printed on the console screen.

Answer Question 2:

Output:

/EnumEx.java:3: error: ',', '}', or ';' expected
    Sun = 101,
        ^
/EnumEx.java:3: error: '}' expected
    Sun = 101,
         ^
/EnumEx.java:4: error: <identifier> expected
    Mon,
       ^
/EnumEx.java:9: error: ';' expected
    Sat
       ^
/EnumEx.java:12: error: class, interface, or enum expected
  public static void main(String[] args) {
                ^
/EnumEx.java:15: error: class, interface, or enum expected
    for (Week w: Vals) {
    ^
/EnumEx.java:17: error: class, interface, or enum expected
    }
    ^
7 errors

Explanation:

The above program will generate syntax error because we cannot assign value to the enum constants in Java.

Answer Question 3:

Output:

/EnumEx.java:2: error: modifier final not allowed here
  final enum Week {
        ^
1 error

Explanation:

The program will generate syntax error because we cannot declare enumerate with the final keyword in Java.

Answer Question 4:

Output:

Sun at 0
Mon at 1
Tue at 2
Wed at 3
Thu at 4
Fri at 5
Sat at 6

Explanation:

In the above program, we created a class EnumEx that contains a static user define enumeration Week and a static method main(). The Week enumeration contains 7-week values.

In the main() method we created an array of Week enum, which is initialized with values of enum and then we accessed each value and indices of Week enum using the foreach loop and printed on the console screen.

Answer Question 5:

Output:

Wednesday

Explanation:

In the above program, we created a class EnumEx that contains a static user define enumeration Week and a static method main(). The Week enumeration contains 7-week values.

In the main() method we created an array of Week enum, which is initialized with values of enum and then we defined a switch-case block and defined case according to indices.

Vals[3].ordinal()

The above statement will return 3 then case 3 will execute and print "Wednesday" on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java find output programs (Autoboxing & Unboxing) ... >>
<< Java find output programs (Enumeration) | set 1...