SCJP 5.0 quiz on Enums
Select all the correct statements regarding Enums (Select any 2 options)
What is the output of the following code:
Options:
What is the output of the following code:
line1> public enum Day {
line2> MONDAY (1),
line3> TUESDAY (2),
line4> WEDNESDAY (3) {public String toString() {
return "Good Morning"; } },
line5> THURSDAY (4),
line6> FRIDAY (5),
line7> SATURDAY (6),
line8> SUNDAY (7);
line9>
line10> int dayNumber;
line11>
line12> Day (int dayNumber) {
line13> this.dayNumber = dayNumber;
line14> }
line15>
line16> public static void main (String[] args) {
line17> for (Day d : Day.values())
line18> System.out.println (d);
line19> }
line20> }
Options:
Examine the following code and select the correct options:
enum Rating {
AVERAGE,
GOOD,
EXCELLENT;
abstract String performance();
}
class Test {
public static void main (String[] args) {
System.out.println (Rating.AVERAGE);
}
}
Examine the following code and select the correct options that follow:
line1> public enum IceCream {
line2> VANILLA ("white"),
line3> STRAWBERRY ("pink"),
line4> WALNUT ("brown"),
line5> CHOCOLATE ("dark brown");
line6>
line7> String color;
line8>
line9> IceCream (String color) {
line10> this.color = color;
line11> }
line12> }
line13>
line14> public class Test {
line15> public static void main (String[] args) {
line16> IceCream vanilla = new IceCream ("white");
line17> System.out.println ( vanilla );
line18> }
line19> }
Select all the incorrect options:
Examine the following code:
line1> enum Size {
line2> XS ("Extra Small") { int age() { return 5; }},
line3> S ("Small") { int age() { return 8; }},
line4> M ("Medium") { int age() { return 10; }},
line5> L ("Large") { int age() { return 15; }},
line6> XL ("Extra Large") { int age() { return 20; }};
line7>
line8> String description;
line9>
line10> Size (String desc) {
line11> this.description = desc;
line12> }
line13>
line14> > // INSERT CODE HERE
line15> }
line16>
line17> class Test {
line18> public static void main (String[] args) {
line19> for (Size size : Size.values())
line20> System.out.println (size);
line21> }
line22> }
Select the correct lines of code, which when inserted at line number 14, will print out the following output:
XS S M L XLOptions:
Examine the following code:
import java.util.*;
enum Colours {
YELLOW (Personality.EXPRESSIVE),
GREEN (Personality.AMIABLE),
RED (Personality.ASSERTIVE),
BLUE (Personality.ANALYTICAL);
Personality personality;
Colours (Personality personality) {
this.personality = personality;
}
enum Personality {ASSERTIVE, EXPRESSIVE, AMIABLE, ANALYTICAL };
}
class TestColours {
public static void main (String[] args) {
// INSERT LINE OF CODE HERE
}
}
Which line of code when replaced with "// INSERT LINE OF CODE HERE", will output a value "true":
Examine the following code and select the correct options:
enum Rating {
POOR (0.0, 5.0),
AVERAGE (5.1, 7.0),
GOOD (7.0, 8.5),
EXCELLENT (8.6, 9.9);
double lowerLimit, upperLimit;
Rating (double ll, double ul) {
this.lowerLimit = ll;
this.upperLimit = ul;
}
int raise() {
switch (this) {
case POOR : return 0;
case AVERAGE : return 5;
case GOOD : return 20;
case EXCELLENT: return 45;
}
return 0;
}
}
class Appraisal {
public static void main (String args[])
throws NumberFormatException {
double currentSalary = 100;
double increment = 0;
for (Rating r : Rating.values()) {
increment = currentSalary/ 100 * r.raise();
System.out.println
(r + " Performance, Revised Salary = "
+ (currentSalary + increment));
}
}
}
Options:
Examine the following code and select the correct options:
enum Shape {
CIRCLE (0, "red"),
TRIANGLE (3),
SQUARE (4),
RECTANGLE (4),
PENTAGON (5),
HEXAGON (6, "yellow"),
OCTAGON (8, "pink");
int numberOfSides;
String shapeColor;
Shape (int sides) { numberOfSides = sides; }
Shape (int sides, String colour) {
numberOfSides = sides;
shapeColor = colour;
}
public static void main (String[] args) {
for (Shape s : Shape.values())
System.out.println(s);
}
}
Options:
Examine the following code:
public enum Day {
MONDAY (1),
TUESDAY (2),
WEDNESDAY (3),
THURSDAY (4),
FRIDAY (5),
SATURDAY (6),
SUNDAY (7);
int dayNumber;
Day (int dayNumber) {
this.dayNumber = dayNumber;
}
// INSERT CODE HERE
public static void main (String[] args) {
for (Day d : Day.values())
System.out.println (d);
}
}
Select from the following options the correct method, which when inserted in the above enum line, will print out the following result:
1 day of week 2 day of week 3 day of week 4 day of week 5 day of week 6 day of week 7 day of weekOptions:
ANSWERSAnswer : c, d
Explanation :
Answer : c
Explanation :
Answer : b
Explanation :
Answer : c
Explanation : The code will fail to compile because the abstract method 'performance' needs to be implemented by all the enum constants, i.e., AVERAGE, GOOD and EXCELLENT.
Answer : d
Explanation : Enums cannot be instantiated. The code mentioned in this question will not compile.
Answer : a, d
Explanation :
Answer: a, b
Explanation : Since all the enum constants, i.e., XS, S, M, L, XL implement the age method, this method can either be defined as a non-abstract method, or as an abstract method.
Options c and d are not valid because the method "short age()" cannot override the method "int age()".
Answer : a
Explanation :
Answers : b, c
Explanation :
Answer : a
Explanation :
Answer : a
Explanation :
FeedbackPlease help us to improve this quiz by submitting your feedback.
|
|