Core Java - Interview Questions and Answers for 'Enums' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Enums' - 22 question(s) found - Order By Newest

 Q1. What are concepts introduced with Java 5 ?Core Java
Ans. Generics , Enums , Autoboxing , Annotations and Static Import.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java5   generics   enum   autoboxing   annotations   static import        rare

Try 1 Question(s) Test


 Q2. Enums cannot be declared ..

a. private
b. public
c. static
d. final
Core Java
Ans. final

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   enum


 Q3. If I try to add Enum constants to a TreeSet, What sorting order will it use ?
Ans. Tree Set will sort the Values in the order in which Enum constants are declared.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   collections   set   treeset   enum   sorting   technical lead


 Q4. Can we override compareTo method for Enumerations ?Core Java
Ans. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   compareto   final methods   enum   enumeration      expert


 Q5. enums are intrinsically ..

a. private
b. public
c. static
d. final
Core Java
Ans. static

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   enum


 Q6. When were Enums introduced in Java ?
Ans. Enums were introduced with java 5.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java5   enum

Try 1 Question(s) Test


 Q7. What is an Enum type ?Core Java
Ans. An enum type is a special data type that enables for a variable to be a set of predefined constants

  Sample Code for enum

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java5   enum   basic interview question      basic        frequent

Try 2 Question(s) Test


 Q8. What will be the output of the following code ?

enum Day {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}

public class BuggyBread1{
public static void main (String args[]) {
Set mySet = new TreeSet();
mySet.add(Day.SATURDAY);
mySet.add(Day.WEDNESDAY);
mySet.add(Day.FRIDAY);
mySet.add(Day.WEDNESDAY);
for(Day d: mySet){
System.out.println(d);
}
}
}
Core Java
Ans. WEDNESDAY
FRIDAY
SATURDAY

Only one FRIDAY will be printed as Set doesn't allow duplicates.Elements will be printed in the order in which constants are declared in the Enum. TreeSet maintains the elements in the ascending order which is identified by the defined compareTo method. compareTo method in Enum has been defined such that the constant declared later are greater than the constants declared prior.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   code   coding   enum   set   treeset   advanced

Try 3 Question(s) Test


 Q9. What is an enumeration?Core Java
Ans. An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in the collection.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   enum   enumeration interface   basic interview question

Try 2 Question(s) Test


 Q10. public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

public class Test {
Set mySet = new HashSet();
mySet.add(Day.MONDAY);
mySet.add(Day.SUNDAY);
mySet.add(Day.SATURDAY);

for(Day d: mySet){
System.out.println(d);
}
}
Ans. SUNDAY
MONDAY
SATURDAY

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum   set   collections   hashset   coding   code

Try 3 Question(s) Test


 Q11. What will be the output of the following code ?

enum Day {
   MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}

public class BuggyBread1{
   public static void main (String args[]) {
      Set mySet = new HashSet();
      mySet.add(Day.SATURDAY);
      mySet.add(Day.WEDNESDAY);
      mySet.add(Day.FRIDAY);
      mySet.add(Day.WEDNESDAY);
      for(Day d: mySet){
         System.out.println(d);
      }
   }
}
Ans. FRIDAY , SATURDAY and WEDNESDAY will be printed but the order cannot be determined.

Only one FRIDAY will be printed as Set doesn't allow duplicates. Order cannot be determined as HashSet doesn't maintain elements in a particular order. 

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   code   coding   enum   set   hashset

Try 3 Question(s) Test


 Q12. Can we extend an Enum ?Core Java
Ans. No. Enums are final by design.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   enums   inheritance  object oriented programming (oops)  oops concepts   yes-no     Asked in 1 Companies      intermediate


 Q13. Shall we keep the enumeration name in all capital letters as it contains all constants ?
Ans. Enum is actually a class with pre defined constants. So enum name should follow the convention of the Class name. Though the constant names or the Enum variables should follow the convention of having all capital letters.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   enum   enumerations   java conventions   java naming conventions


 Q14. In Hibernate, While defining Criteria, Have you ever faced any problem while adding restrictions with user defined types or Enums ?Hibernate
Ans. Yes, with Enum as was getting an exception while doing equality check for enum field.

got it fixed by adding @Enumerated(EnumType.STRING) to the field in entity.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     hibernate   hibernate criteria   enumerations


 Q15. Name few Enum classes and interfaces provided by Java ?
Ans. http://www.buggybread.com/2015/01/java-enum-classes-and-interfaces.html

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   enums


 Q16. If we add Enum constants to a sorted collection ( Treemap , TreeSet ), What will be the order in which they will be maintained ?

a. Sorted Collection wont maintain them in any order.
b. Insertion Order
c. Order in which constants are declared.
d. Natural Sorting Order.
Ans. Order in which constants are declared.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum   collections   sorted collections   treemap   treeset   compareto   comparable   java


 Q17. Can Enum be a replacement for a constant file in majority of the situation ?Design
Ans. Yes , it can be but it may not be an optimal alternative if we need to change the values frequently. As Enum is compiled like any Java File , any change requires the application to be rebuild , deployed and restart.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum  design


 Q18. What is the difference between Enumeration and Iterator ?Core Java
Ans. Enumeration can iterate only legacy collections like Vector , HashTable and Stack whereas Iterator can iterate both legacy and non legacy collections.

Enumeration is less safer than Iterator

Enumeration is fail safe whereas Iterator is fail fast

Iterator allows for removal of element while traversal whereas Enumeration doesn't have remove method.

Enumerations were introduced in Java 1 whereas Iterators were introduced with Java 2

Enumerations have methods like hasMoreElements and nextElement whereas Iterators have methods like hasNext, next and remove

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enumeration vs iterator  collections      Basic        frequent


 Q19. Can an enum extend another enum ?Core Java
Ans. No

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum


 Q20. Can we have a sub enum within an enum ?Core Java
Ans. Yes

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum  enum within enum


 Q21. What will happen if there are no enums in Java ?Core Java
Ans. Enums in Java is a facility to have objects upfront and use them as constants. Even if such a facility is not available , a workaround could be achieved.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum


 Q22. How is the declaration of enum in Typescript differ from Javascript ?TypeScript
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     enum typescript  enum javascript



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: