|
SCJP : Type Safe Enum Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public enum Test {
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Test(int hh, int mm) {
assert (hh >= 0 && hh <= 23) : "Illegal hour.";
assert (mm >= 0 && mm <= 59) : "Illegal mins.";
this.hh = hh;
this.mm = mm;
}
public int getHour() {
return hh;
}
public int getMins() {
return mm;
}
public static void main(String args[]){
Test t = new BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
options
A)7:30
B)Compile Error - an enum cannot be instantiated using the new operator.
C)12:50
D)19:45
Correct answer is : B
Explanations : As an enum cannot be instantiated using the new operator, the constructors cannot be called explicitly.
You have to do like
Test t = BREAKFAST;
Questions no -2
What is the output for the below code ?
public class Test {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
enum Month {
JAN, FEB
}
public static void main(String[] args) {
int[] freqArray = { 12, 34, 56, 23, 5, 13, 78 };
// Create a Map of frequencies
Map ordinaryMap = new HashMap();
for (Day day : Day.values()) {
ordinaryMap.put(day, freqArray[day.ordinal()]);
}
// Create an EnumMap of frequencies
EnumMap frequencyEnumMap = new EnumMap(ordinaryMap);
// Change some frequencies
frequencyEnumMap.put(null, 100);
System.out.println("Frequency EnumMap: " + frequencyEnumMap);
}
}
options
A)Frequency EnumMap: {MONDAY=12, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=78}
B)Compile Error
C)NullPointerException
D)Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=123}
Correct answer is : C
Explanations : The null reference as a key is NOT permitted.
Questions no -3
public class EnumTypeDeclarations {
public void foo() {
enum SimpleMeal {
BREAKFAST, LUNCH, DINNER
}
}
}
Is the above code Compile without error ?
options
A)Compile without error
B)Compile with error
C)Compile without error but Runtime Exception
D)Compile without error but Enum Exception
Correct answer is : B
Explanations :
An enum declaration is a special kind of class declaration:
a) It can be declared at the top-level and as static enum declaration.
b) It is implicitly static, i.e. no outer object is associated with an enum constant.
c) It is implicitly final unless it contains constant-specific class bodies, but it can implement interfaces.
d) It cannot be declared abstract unless each abstract method is overridden in the constant-specific class body of every enum constant.
e) Local (inner) enum declaration is NOT OK!
Here in
public void foo() {
enum SimpleMeal {
BREAKFAST, LUNCH, DINNER
}
}
enum declaration is local within method so compile time error.
Questions no -4
What is the output for the below code ?
public enum Test {
int t;
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Test(int hh, int mm) {
assert (hh >= 0 && hh <= 23) : "Illegal hour.";
assert (mm >= 0 && mm <= 59) : "Illegal mins.";
this.hh = hh;
this.mm = mm;
}
public int getHour() {
return hh;
}
public int getMins() {
return mm;
}
public static void main(String args[]){
Test t = BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
options
A)7:30
B)Compile Error
C)12:15
D)19:45
Correct answer is : B
Explanations : The enum constants must be declared before any other declarations in an enum type.
In this case compile error because of declaration int t; before enum declaration.
| |