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;
}
}
}
Answer Question 1:
Output:
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:
Explanation:
The above program will generate syntax error because we cannot assign value to the enum constants in Java.
Answer Question 3:
Output:
Explanation:
The program will generate syntax error because we cannot declare enumerate with the final keyword in Java.
Answer Question 4:
Output:
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:
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