805 OCPJP 6 Questions 788 SCJP 5 questions 650 OCPJWCD 5 questions 600 OCAJP 7 questions 610 OCPJP 7 questions 510 Upgrade to OCPJP 7 questions 900 Java J2EE Qns

Tutorials

SCJP : Type Safe Enum

The enum declaration is a special kind of class declaration. An enum type has public, self-typed members for each of the named enum constants. All enum classes have high-quality toString, hashCode, and equals methods. All are Serializable, Comparable and effectively final. None are Cloneable. All of the "Object methods" except toString are final.

Arbitrary fields may be added to enum classes, and to individual enum constants. The ability to add such fields will only be used by relatively sophisticated programmers, but greatly enhances the power of the facility. It adds nothing to the complexity of the facility for programmers who don't need the extra power.

public enum Month {
	JAN("January month"),
	FEB("Feb month"),
	MARCH("March month");
	private String displayName;
	Color(String displayName){
		this.displayName = displayName;
	}
	public String toString(){
		return displayName;
	}
}

class MonthTest{
	public static void main(String[] args) {
		Month janMonth = Month.JAN;
		System.out.println(janMonth);
		Month marchMonth = Month.MARCH;
		System.out.println(marchMonth);
	}
}
					
The output:
January month
March month
		
					

SCJP 1.5 SCJP 1.6 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.
scjp 1.5 | scjp 1.6 | scwcd 1.5
Java Certifications
www.javacertifications.net