Friday, March 27, 2020

15 Coffee Enum Interview Questions For Developers Amongst Answers

Enum was introduced inwards Java v in addition to since thence it's been rattling pop alongside Java developers in addition to widely used inwards unlike Java applications. Since Enum inwards Java is much to a greater extent than versatile than Enum inwards C or C++, it likewise presents lots of interesting utilisation cases, duad of them, nosotros receive got seen inwards my article 10 ways to utilisation Enum inwards Java. But, despite beingness thence popular, many Java programmer are even thence non aware of functionality provided past times Enum in addition to subtle details of using Enum inwards Java code. I realized this fact, when duad of my readers asked me unopen to of the questions e.g. Can Enum implement an interface inwards Java or Why nosotros tin non create Enum instances exterior of Enum, stating that these has been asked to them inwards in that place Java Interviews. This motivates me to seat together a listing of oftentimes asked query inwards Java Enum, which non alone helps to practise good inwards Interviews, but likewise opened upwards novel path for learning. As I had said before, lot of fourth dimension a query inwards Interviews, makes you lot to convey a theme to a greater extent than seriously than otherwise, which is non a bad thing, in addition to given the ability in addition to facilities offered past times Java Enum, I think it's high fourth dimension to acquire master copy of it.



Java Enum Interview Questions with Answers

s been rattling pop alongside Java developers in addition to widely used inwards unlike Java applications fifteen Java Enum Interview Questions for Developers with AnswersHere is my listing of questions based on unlike features in addition to properties of Java Enum. You tin utilisation this listing for preparing Interview or exactly equally FAQ of Enum. If you lot are novel to Java than I am for sure you lot volition larn a lot close Enum in addition to it’s useful feature.




Question 1) Can Enum implement interface inwards Java?
Yes, Enum tin implement interface inwards Java. Since enum is a type, similar to class in addition to interface, it tin implement interface. This gives a lot of flexibility to utilisation Enum equally specialized implementation inwards unopen to cases. See here for an illustration of Enum implementing an interface inwards Java.


Question 2) Can Enum extends course of written report inwards Java?
No, Enum tin non extend course of written report inwards Java. Surprised, because I exactly said it's a type similar a class or interface inwards Java. Well, this is why this query is a adept follow-up query of previous Enum interview question. Since all Enum past times default extend abstract base of operations course of written report java.lang.Enum, apparently they tin non extend unopen to other class, because Java doesn't back upwards multiple inheritance for classes. Because of extending java.lang.Enum class, all enum gets methods similar ordinal(), values() or valueOf().


Question 3) How practise you lot create Enum without whatever instance? Is it possible without compile fourth dimension error?
This is ane of those tricky Java question, which Interviewer honey to ask. Since Enum is viewed equally a collection of good defined fixed number of instances e.g. Days of Week, Month inwards a Year, having an Enum without whatever instance, may seems awkward. But yes, you lot tin create Enum without whatever instance inwards Java, say for creating a utility class. This is unopen to other innovative way of using Enum inwards Java. Here is the code

public enum MessageUtil{ ;  // required to avoid compiler error, likewise signifies no instance  public static boolean isValid() {         throw new UnsupportedOperationException("Not supported yet."); }  }


Question 4) Can nosotros override toString() method for Enum? What happens if nosotros don't?
Ofcourse you tin override toString inwards Enum, equally similar whatever other course of written report it likewise extends java.lang.Object in addition to has toString() method available, but fifty-fifty if you lot don't override, you lot volition non going to regret much, because abstract base of operations course of written report of enum does that for you lot in addition to render name, which is call of the enum instance itself. hither is the code of toString() method from Enum course of written report :

 public String toString() {         return name;  }

name is set, when compiler emit code for creating enum inwards answer to instance proclamation inwards enum course of written report itself, along with setting ordinal, equally visible inwards this constructor of enum from java.lang.Enum course of written report :

protected Enum(String name, int ordinal) {         this.name = name;         this.ordinal = ordinal; }

This is the alone constructor of creating enum, which is called past times code, generated past times compiler inwards answer to enum type proclamation inwards Java program.


Question 5) Can nosotros create instance of Enum exterior of Enum itself? If Not, Why?
No, you lot tin non create enum instances exterior of Enum boundry, because Enum doesn't receive got whatever public constructor, in addition to compiler doesn't allow you lot to render whatever world constructor inwards Enum. Since compiler generates lot of code inwards answer to enum type declaration, it doesn’t allow world constructors within Enum, which enforces declaring enum instances within Enum itself.


Question 6) Can nosotros declare Constructor within Enum inwards Java?
This is asked along with previous query on Java Enum. Yes, you lot can, but recall you lot tin alone declare either someone or package-private constructor within enum. world in addition to protected constructors are non permitted within enum. See here for a code example.


Question 7) What is departure inwards comparison Enum with == in addition to equals() method?
I receive got already discussed this query inwards my post service with similar title, encounter here.


Question 8) What does ordinal() method practise inwards Enum?
Ordinal method returns the firm inwards which Enum instance are declared within Enum. For illustration inwards a DayOfWeek Enum, you lot tin declare days inwards firm they come upwards e.g.

public enum DayOfWeek{   MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; }

here if nosotros telephone outcry upwards DayOfWeek.MONDAY.ordinal() it volition render 0, which way it's the get-go instance. This ordering tin live on rattling useful to stand upwards for actual existent footing ordering i.e. declaring TUESDAY afterwards MONDAY, ensures that it came afterwards MONDAY in addition to earlier WEDNESDAY. Similarly you lot tin utilisation enum to stand upwards for Month of twelvemonth inwards the firm they come upwards e.g. FEBRUARY afterwards JANUARY in addition to earlier MARCH. All user defined enum inherit this method from java.lang.Enum abstract class, in addition to it's laid past times compiler, when it internally telephone outcry upwards protected constructor of java.lang.Enum, which accepts call in addition to ordinal.


Question 9) Can nosotros utilisation Enum with TreeSet or TreeMap inwards Java?
This is actually interesting query on Java Enum, I would honey to enquire this to approximate noesis of Enum. Until you lot know close java.lang.Enum in addition to has looked it's code, it's to a greater extent than probable that you lot don't know that Enum implements Comparable interface, which is primary requirement to live on used inwards Sorted Collection similar TreeSet in addition to TreeMap. Since Enum past times default impalement Comparable interface, they tin live on safely used within TreeSet or TreeMap inwards Java.


Question 10) What is departure betwixt ordinal() in addition to compareTo() inwards Enum?
This is follow-up of previous query on Java Enum. Actually, compareTo() mimic ordering provided past times ordinal() method, which is the natural firm of Enum. In curt Enum constraints are compared inwards the firm they are declared. Also, worth remembering is that enum constants are alone comparable to other enum constants of the same enum type. Comparing enum constant  of ane type to unopen to other type volition upshot inwards compiler error.


Question 11) Can nosotros utilisation Enum inwards switch instance inwards Java?
Yes, you lot tin utilisation Enum inwards Switch instance inwards Java, inwards fact that's ane of the primary wages of using Enum. Since Enum instances are compile fourth dimension constant, you lot tin safely utilisation them within switch in addition to case statements. Here is an illustration of using our DayOfWeek enum inwards switch instance :

public void developerState(DayOfWeek today){         switch(today){             case MONDAY:                 System.out.println("Hmmmmmmmm");                 break;             case TUESDAY:                 System.out.println("Hmmmm");                 break;             case FRIDAY :                 System.out.println("Yeahhhhhh");                 break;         }      }

Enum in addition to Switch cases goes good with each other, peculiarly if Enum has relatively minor number of fixed constants e.g. vii days inwards week, 12 months inwards a twelvemonth etc, See hither for unopen to other illustration of using switch instance with Enum inwards Java.


Question 12) How to iterate over all instance of a Enum?
Well, if you lot receive got explored java.lang.Enum, you lot know that in that place is a values() method which returns an array of all enum constant. Since every enum type implicitly extends java.lang.Enum, they acquire this values() method. By using, this you lot tin iterate over all enum constants of a for sure type. See hither for a Enum values Example inwards Java for iterating over Enum using values() in addition to foreach loop.


Question 13) What is wages in addition to disadvantage of using Enum equally Singleton?
Enum provides you lot a quick shortcut to implement Singleton blueprint pattern, in addition to always since it's mentioned inwards Effective Java, it's been a pop alternative equally well. On the face, Enum Singleton looks rattling promising in addition to handles lot of materials for you lot e.g. controlled instance creation, Serialization security in addition to on reach of that, it’s extremely slow to create thread-safe Singleton using Enum. You don’t demand to worry close double checked locking in addition to volatile variable anymore. See here to know close pros in addition to cons of using Enum equally Singleton inwards Java.


Question 14) What is wages of using Enum over enum int pattern in addition to enum String pattern?
If you lot are coding from to a greater extent than than v years, in addition to receive got coded inwards JDK 1.3 in addition to 1.4, you lot must live on familiar with Enum String pattern in addition to enum int pattern, where nosotros used public static concluding constants to stand upwards for collection of good known fixed number of things e.g. DayOfWeek. There was lot of work with that approach e.g. you lot don't receive got a dedicated enum type, Since it's String variable, which stand upwards for solar daytime of week, it tin convey whatever arbitrary value. Similarly enum int pattern tin convey whatever arbitrary value, compiler doesn't forbid those. By using Enum, you lot acquire this type-safety in addition to compiler checking for you. There are duad of adept items on this theme inwards Effective Java, which is in ane lawsuit again, must read for whatever Java developer.


Question 15) How to convert an String to Enum inwards Java?
This is a solar daytime to solar daytime ask, given popularity of String in addition to Enum inwards Java application development. Best way for converting Enum to String, is to declare a mill method within Enum itself, which should convey String statement in addition to render an Enum. You tin lead to ignore instance equally well. See hither for a code illustration of String to Enum conversion inwards Java.


That's all on this list of Java v Enum Interview Questions in addition to Answers. Just recall though, reading is non plenty for learning, it's exactly a get-go step. In firm to acquire proficient with enum, effort to discovery out where tin you lot utilisation Enum inwards your project? This volition give you lot REAL experience, in addition to with existent sense you lot larn lot to a greater extent than than a sample application, because you lot tend to confront to a greater extent than issues, handgrip rather complex in addition to detailed requirement. Nevertheless, this Java v Enum questions are even thence worth for revising your knowledge, peculiarly if you lot are rushing for an Interview, in addition to don't receive got plenty fourth dimension to explore Enum inwards detail.

Related Java Interview Tutorials from blog

No comments:

Post a Comment