IDL-to-Java mapping for enums - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. How will the UserType enum in the following IDL map to Java ?
module Module4
{
 enum UserType {admin, user, guest};
};

Please select the best answer.
  A.
package Module4;
public final class UserType
 implements org.omg.CORBA.portable.IDLEntity
{
 private int value = -1;
 public static final int _admin = 0;
 public static final UserType admin = new UserType(_admin);
 public static final int _user = 1;
 public static final UserType user = new UserType(_user);
 public static final int _guest = 2;
 public static final UserType guest = new UserType(_guest);
 public int value()
 {
  return value;
 }
 public static UserType from_int(int value)
 {
  switch (value) {
   case _admin: return admin;
   case _user: return user;
   case _guest: return guest;
   default: return null;
  }
 }
 public UserType(int i)
 {
  value = i;
 }

}
  B.
package Module4;
public final class UserType
 implements org.omg.CORBA.portable.IDLEntity
{
 private int value = -1;
 public static final int _admin = 0;
 public final UserType admin = new UserType(_admin);
 public static final int _user = 1;
 public final UserType user = new UserType(_user);
 public static final int _guest = 2;
 public final UserType guest = new UserType(_guest);
 public int value()
 {
  return value;
 }
 public UserType from_int(int value)
 {
  switch (value) {
   case _admin: return admin;
   case _user: return user;
   case _guest: return guest;
   default: return null;
  }
 }
 private UserType(int i)
 {
  value = i;
 }
}
  C.
package Module4;
public final class UserType
 implements org.omg.CORBA.portable.IDLEntity
{
 private int value = -1;
 public static final int _admin = 0;
 public static final UserType admin = new UserType(_admin);
 public static final int _user = 1;
 public static final UserType user = new UserType(_user);
 public static final int _guest = 2;
 public static final UserType guest = new UserType(_guest);
 public int value()
 {
  return value;
 }
 public static UserType from_int(int value)
 {
  switch (value) {
   case _admin: return admin;
   case _user: return user;
   case _guest: return guest;
   default: return null;
  }
 }
 private UserType(int i)
 {
  value = i;
 }
}
  The correct answer is C. The correct answer is C, because it adheres to the mapping. A is incorrect because the constructor is public, which would make it possible to instantiate nonsingleton enums, which would remove the guarantee that enums of the same value would be equal (==). B is incorrect because the member objects are not static as required. This not only would damage enum equality, but would also make it impossible to have any enum objects because the constructor is not accessible.

2. Which is a correct code fragment to return a guest UserType object given the following IDL ?
module Module4
{
enum UserType {admin, user, guest};
};

Please select the best answer.
  A. return new UserType(UserType._guest);
  B. return UserType.guest;
  C. return UserType.guest.value()
  The correct answer is B. B is correct because it returns the singleton guest object. A is incorrect because the constructor is not public. C is incorrect because it returns the int index value for the object, not the object itself.

3. Will the java.lang.Object equals() and hash() methods work correctly for enum objects in Java ?
Please select the best answer.
  A. equals() does not work correctly, hash() does not work correctly.
  B. equals() does not work correctly, hash() does work correctly.
  C. equals() does work correctly, hash() does not work correctly.
  D. equals() does work correctly, hash() does work correctly.

The correct answer is D. There is only one (singleton) instance of any given enum object value. Thus, == works and so do the default java.lang.Object implementations of equals() and hash(). A, B, and C are all incorrect for this reason.