SCJP Questions

SCWCD Questions

Friday, April 10, 2009

SCJP Mock Questions : Type Safe Enum

********************************************************************
original from www.techfaq360.com
SCJP Mock Questions : Type Safe Enum
********************************************************************
Question No :1
What will be the output of the following program?
public enum Color {
RED("Red Color"),
GREEN("Blue Color"),
BLUE("Blue Color");
private String displayName;
Color(String displayName){
this.displayName = displayName;
}
public String toString(){
return displayName;
}
}
class ColorTest{
public static void main(String[] args) {
Color redColor = Color.RED;
System.out.println(redColor);
Color blueColor = Color.BLUE;
System.out.println(blueColor);
}
}
(Choose correct one from multiple below)
1. The program will compile and throw a run-time exception
2. The program won't compile as it is not possible to define methods within an Enum.
3. The program will compile and the output will be 'Red Color' followed by 'Blue Color'.
4. None of the above
correct is :3
Explanations :defines a Enum called Color and the Enum constants within are parameterized by
defining a constructor for holding the display name of each constants. The method toString() is
defined at the declaration level which means that when an Enum object is printed, this method will
get called.
------------------------------------------------------------------------------
Question No :2
Which statement is true about enum declaration ?
(Choose correct one from multiple below)
1. can be declared at the top-level and as static enum declaration
2. implicitly static, i.e. no outer object is associated with an enum constant
3. implicitly final unless it contains constant-specific class bodies, but it can implement interfaces
4. All of the above
correct is :4
Explanations :Properties of the enum type
An enum declaration is a special kind of class declaration:
It can be declared at the top-level and as static enum declaration.
It is implicitly static, i.e. no outer object is associated with an enum constant.
It is implicitly final unless it contains constant-specific class bodies, but it can implement interfaces.
It cannot be declared abstract unless each abstract method is overridden in the constant-specific
class body of every enum constant
------------------------------------------------------------------------------
Question No :3
public class EnumTypeDeclarations {
public enum SimpleMeal {
BREAKFAST, LUNCH, DINNER
};
}
Is the above code compile without error?
(Choose correct one from multiple below)
1. true
2. false
3. Compile without error but runtime exceptions
4. None of the above
correct is :1
Explanations :Static enum declaration is OK.
------------------------------------------------------------------------------
Question No :4
public class EnumTypeDeclarations {
public void foo() {
enum SimpleMeal {
BREAKFAST, LUNCH, DINNER
}
}
}
Is the above code Compile without error ?
(Choose correct one from multiple below)
1. Compile without error
2. Compile with error
3. Compile without error but Runtime Exception
4. None of the above
correct is :2
Explanations :Local (inner) enum declaration is NOT OK!
------------------------------------------------------------------------------
Question No :5
Which statement is true about Enum constructors ?
(Choose correct one from multiple below)
1. Each constant declaration can be followed by an argument list that is passed to the constructor
of the enum type having the matching parameter signature.
2. An implicit standard constructor is created if no constructors are provided for the enum type.
3. As an enum cannot be instantiated using the new operator, the constructors cannot be called
explicitly.
4. All of the above
correct is :4
Explanations :Enum constructors
Each constant declaration can be followed by an argument list that is passed to the constructor of
the enum type having the matching parameter signature.
An implicit standard constructor is created if no constructors are provided for the enum type.
As an enum cannot be instantiated using the new operator, the constructors cannot be called
explicitly.
Example of enum constructors:
public enum Meal {
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
private int mm;
Meal(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;
}
}
------------------------------------------------------------------------------
Question No :6
What is the output of the bellow 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 = BREAKFAST;
System.out.println(t.getHour() +":"+t.getMins());
}
}
(Choose correct one from multiple below)
1. 7:30
2. Compile With Error
3. Compile Without Error but Runtime Exception
4. None of the above
correct is :2
Explanations :
All the enum static like BREAKFAST treat as constructor.
Enum constructors
Each constant declaration can be followed by an argument list that is passed to the constructor of
the enum type having the matching parameter signature.
An implicit standard constructor is created if no constructors are provided for the enum type.
As an enum cannot be instantiated using the new operator, the constructors cannot be called
explicitly.
Output is : 7:30
------------------------------------------------------------------------------
Question No :7
hat is the output of the bellow 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());
}
}
(Choose correct one from multiple below)
1. 7:30
2. Compile with Errors
3. Runtime Exceptions
4. None of the above
correct is :2
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;
------------------------------------------------------------------------------
Question No :8
What is the output ?
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 Test(12,45);
System.out.println(t.getHour() +":"+t.getMins());
}
}
(Choose correct one from multiple below)
1. 12:45
2. Compile With Error
3. Runtime Exception
4. None of the above
correct is :2
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;
------------------------------------------------------------------------------
Question No :9
Is the bellow code compile without error ?
public enum Meal {
int q = 1;
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
}
(Choose correct one from multiple below)
1. Yes , compile without error
2. No, compile with error
3. compile without error but Runtime Exception
4. None of the above
correct is :2
Explanations :the enum constants must be declared before any other declarations in an enum type
public enum Meal {
int q = 1; // WRONG ! Compilation error !
BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45);
private int hh;
...
}
------------------------------------------------------------------------------
Question No :10
Which statement is true about Enums ?
(Choose correct one from multiple below)
1. Enums cannot declare methods which override the final methods of the java.lang.Enum class
like clone(), compareTo(Object), equals(Object) etc.
2. that the enum constants must be declared before any other declarations in an enum type.
3. both 1 and 2 is true
4. None of the above
correct is :3
Explanations :Enums cannot declare methods which override the final methods of the
java.lang.Enum class:
clone(), compareTo(Object), equals(Object), getDeclaringClass(), hashCode(), name(), ordinal().
The final methods do what their names imply, but the clone() method throws an
CloneNotSupportedException, as an enum constant cannot be cloned.
Note that the enum constants must be declared before any other declarations in an enum type.
------------------------------------------------------------------------------
Question No :11
Is the bellow code compile without error ?
public enum Meal {
BREAKFAST(7, 30) {
public double mealPrice() {
double breakfastPrice = 10.50;
return breakfastPrice;
}
}
final double mealPrice() { return 0; }
}
(Choose correct one from multiple below)
1. Compile with error
2. Compile without error
3. None of the above
4. None of the above
correct is :1
Explanations :WRONG! Compilation error! Cannot override the final method from Meal.
An enum type that contains constant-specific class bodies implicitily final:
------------------------------------------------------------------------------
Question No :12
What is the output of the bellow code ?
public enum Test {
BREAKFAST(7, 30){
public double final mealPrice() {
double breakfastPrice = 10.50;
return breakfastPrice;
}
}
, 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());
}
}
(Choose correct one from multiple below)
1. 7:30
2. Compile with error
3. Runtime Exceptions
4. None of the above
correct is :2
Explanations :An enum type that contains constant-specific class bodies cannot be declared final:
------------------------------------------------------------------------------
Question No :13
Which is the bellow statement is true about java.util.EnumSet ?
(Choose correct one from multiple below)
1. All of the elements in an enum set MUST come from a single enum type.
2. The null reference CANNOT be stored in an enum set.
3. all methods are static except for the clone() method.
4. All of the above
correct is :4
Explanations :java.util.EnumSet
Enums can be used in a special purpose Set implementation (EnumSet) which provides better
performance.
All of the elements in an enum set MUST come from a single enum type.
Enum sets are represented internally as bit vectors.
The EnumSet class defines new methods and inherits the ones in the Set/Collection interfaces.
NOTE, all methods are static except for the clone() method.
All methods return an EnumSet.
The null reference CANNOT be stored in an enum set.
------------------------------------------------------------------------------
Question No :14
What is the output for the bellow code ?
import java.util.EnumSet;
import static java.lang.System.out;
public class UsingEnumSet {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumSet allDays = EnumSet.range(Day.MONDAY, Day.SUNDAY);
System.out.println("All days: " + allDays);
}
(Choose correct one from multiple below)
1. All days: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
2. All days: [MONDAY, SUNDAY]
3. Compile with error
4. None of the above
correct is :1
Explanations :EnumSet.range(from, to) : Creates an enum set initially containing all of the
elements in the range defined by the two specified endpoints
------------------------------------------------------------------------------
Question No :15
Is the bellow statement is true ?
The null reference CANNOT be stored in an enum set.
(Choose correct one from multiple below)
1. true
2. false
3. Can't say
4. None of the above
correct is :1
Explanations :java.util.EnumSet
Enums can be used in a special purpose Set implementation (EnumSet) which provides better
performance.
All of the elements in an enum set MUST come from a single enum type.
Enum sets are represented internally as bit vectors.
The EnumSet class defines new methods and inherits the ones in the Set/Collection interfaces.
NOTE, all methods are static except for the clone() method.
All methods return an EnumSet.
The null reference CANNOT be stored in an enum set.
------------------------------------------------------------------------------
Question No :16
What is the output of bellow code ?
import java.util.EnumSet;
import static java.lang.System.out;
public class UsingEnumSet {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumSet oddDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY,
Day.FRIDAY, Day.SUNDAY);
out.println("Odd days: " + oddDays);
EnumSet evenDays = EnumSet.complementOf(oddDays);
out.println("Even days: " + evenDays);
}
}
(Choose correct one from multiple below)
1. Compile with error
2. Odd days: [MONDAY, WEDNESDAY, FRIDAY, SUNDAY]
Even days: [TUESDAY, THURSDAY, SATURDAY]
3. Odd days: [MONDAY, WEDNESDAY, FRIDAY, SUNDAY]
Even days: [MONDAY, WEDNESDAY, FRIDAY, SUNDAY]
4. None of the above
correct is :2
Explanations :EnumSet.of() :Creates an enum set initially containing the specified element.
EnumSet.complementOf() :Creates an enum set with the same element type as the specified
enum set, initially containing all the elements of this type that are not contained in the specified
set.
------------------------------------------------------------------------------
Question No :17
What is true about java.util.EnumMap ?
(Choose correct one from multiple below)
1. The null reference as a key is NOT permitted.
2. All of the keys in an enum map MUST come from a single enum type.
3. Enum maps are maintained in the natural order of their keys
4. All of the above
correct is :4
Explanations :java.util.EnumMap
Enums can be used in a special purpose Map implementation (EnumMap) which provides better
performance.
Enum maps are represented internally as arrays.
All of the keys in an enum map MUST come from a single enum type.
Enum maps are maintained in the natural order of their keys (i.e. the order of the enum constant
declarations).
The EnumMap class re-implements most of the methods in the Map interface.
The null reference as a key is NOT permitted.
------------------------------------------------------------------------------
Question No :18
Is the bellow statement is true for java.util.EnumMap ?
The null reference as a key is NOT permitted.
(Choose correct one from multiple below)
1. true
2. false
3. Can't day
4. None of the above
correct is :1
Explanations :The null reference as a key is NOT permitted.
------------------------------------------------------------------------------
Question No :19
Is the bellow statement is true for java.util.EnumMap ?
"All of the keys in an enum map MUST come from a single enum type"
(Choose correct one from multiple below)
1. true
2. false
3. Can't say
4. None of the above
correct is :1
Explanations :java.util.EnumMap
Enums can be used in a special purpose Map implementation (EnumMap) which provides better
performance.
Enum maps are represented internally as arrays.
All of the keys in an enum map MUST come from a single enum type.
Enum maps are maintained in the natural order of their keys (i.e. the order of the enum constant
declarations).
The EnumMap class re-implements most of the methods in the Map interface.
The null reference as a key is NOT permitted.
------------------------------------------------------------------------------
Question No :20
What is the output for the bellow code ?
public class Test {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
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(Day.MONDAY, 100);
frequencyEnumMap.put(Day.FRIDAY, 123);
System.out.println("Frequency EnumMap: " + frequencyEnumMap);
}
}
(Choose correct one from multiple below)
1. Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56, THURSDAY=23,
FRIDAY=123, SATURDAY=13, SUNDAY=78}
2. Frequency EnumMap: {MONDAY=12, TUESDAY=34, WEDNESDAY=56, THURSDAY=23,
FRIDAY=5, SATURDAY=13, SUNDAY=78}
3. Compile with error
4. None of the above
correct is :1
Explanations :// Creates an empty enum map with the specified key type.
EnumMap(Class keyType)
// Creates an enum map with the same key type as the specified enum map, initially
// containing the same mappings (if any).
EnumMap(EnumMap m)
// Creates an enum map initialized from the specified map.
EnumMap(Map m)
------------------------------------------------------------------------------
Question No :21
What is the output for bellow code ?
public class Test {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
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("abc", 100);
frequencyEnumMap.put("bcd", 123);
System.out.println("Frequency EnumMap: " + frequencyEnumMap);
}
}
(Choose correct one from multiple below)
1. Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56, THURSDAY=23,
FRIDAY=123, SATURDAY=13, SUNDAY=78}
2. Frequency EnumMap: {abc=100,bcd=123,MONDAY=100, TUESDAY=34, WEDNESDAY=56,
THURSDAY=23, FRIDAY=123, SATURDAY=13, SUNDAY=78}
3. Compile with error
4. None of the above
correct is :3
Explanations :All of the keys in an enum map MUST come from a single enum type.
------------------------------------------------------------------------------
Question No :22
What is the output for the bellow 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(Month.JAN, 100);
System.out.println("Frequency EnumMap: " + frequencyEnumMap);
}
}
(Choose correct one from multiple below)
1. Frequency EnumMap: {JAN=100,MONDAY=100, TUESDAY=34, WEDNESDAY=56,
THURSDAY=23, FRIDAY=123, SATURDAY=13, SUNDAY=78}
2. Compile with error
3. Runtime Exception
4. None of the above
correct is :2
Explanations :All of the keys in an enum map MUST come from a single enum type.
EnumMap is trying to add key from both the enums(Day and Month).
------------------------------------------------------------------------------
Question No :23
What is the output for the bellow 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);
}
}
(Choose correct one from multiple below)
1. Compile with error
2. NullPointerException
3. Successfully run
4. None of the above
correct is :2
Explanations :The null reference as a key is NOT permitted. so
Exception in thread "Main Thread" java.lang.NullPointerException:
at java.util.EnumMap.typeCheck(EnumMap.java:645)
at java.util.EnumMap.put(EnumMap.java:231)
------------------------------------------------------------------------------
Question No :24
What is the output for the bellow code ?
public class Test {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumSet allDays = EnumSet.range(Day.MONDAY, Day.SUNDAY);
allDays.add(null);
System.out.println("All days: " + allDays);
}
}
(Choose correct one from multiple below)
1. All days: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
2. All days: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,
SUNDAY,null]
3. java.lang.NullPointerException:
4. None of the above
correct is :3
Explanations :The null reference CANNOT be stored in an enum set.
Exception in thread "Main Thread" java.lang.NullPointerException:
at java.util.EnumSet.typeCheck(EnumSet.java:360)
at java.util.RegularEnumSet.add(RegularEnumSet.java:141)
at java.util.RegularEnumSet.add(RegularEnumSet.java:18)
------------------------------------------------------------------------------
Distribution of this pdf is illegal without permission of techfaq360.com

No comments:

Post a Comment