EXCEPTION HANDLING

Question 1 : which option is true?

class Employee
{
  int empid;
  String name;
  Employee(int id,String name)
  {
  this.empid=id;
  this.name=name;
  }

  public int getEmpId()
  {
          return empid;
  }


  public String getString()
  {
          return name;
  }

  boolean equals(Object o)
 {
         if ((o instanceof Employee) && (((Employee)o).getEmpId()
         == this.empid))
   {
            return true;
         }
   else
   {
   return false;
         }
  }


}
class  JavaBeat1
{
 public static void main(String[] args)
 {
  Employee x1 = new Employee(1,"abc");
  Employee x2 = new Employee(2,"def");
  Employee x3 = new Employee(1,"abc");
  if(  x1.equals(x3) )
  {
    System.out.println("x1 and x3 are equal");
  }
  else
  {
    System.out.println("x1 and x3 are not equal");
  }


 }
}

1. compile Time error.
2. Runtime Error.
3. x1 and x3 are equal.
4. x1 and x3 are not equal.

Question 2 : which statement are true?

class Employee
{
  int empid;
  String name;
  Employee(int id,String name)
  {
  this.empid=id;
  this.name=name;
  }

  public int getEmpId()
  {
          return empid;
  }


  public String getString()
  {
          return name;
  }

  public  boolean equals(Employee o)
  {
         if ((o instanceof Employee) && (((Employee)o).getEmpId() == this.empid))
   {
            return true;
         }
   else
   {
   return false;
         }
  }


}
class  JavaBeat2
{
 public static void main(String[] args)
 {
  Employee x1 = new Employee(1,"abc");
  Employee x2 = new Employee(2,"def");
  Employee x3 = new Employee(1,"abc");
  if(  x1.equals(x3) )
  {
    System.out.println("x1 and x3 are equal");
  }
  else
  {
    System.out.println("x1 and x3 are not equal");
  }

 }
}

1. compile Time error.
2. Runtime Error.
3. x1 and x3 are equal.
4. x1 and x3 are not equal.
5. equals method in class Employee is legal overridding of equals method in class Object

Question 3 : which variable of class should not be used in hashCode method?
1. private
2. transient
3. final
4. public

class ABC

  transient  int x=9;

   int hashCode()
  {
     return x;
   }
}

(for eg consider value of variable x as 9. x is transient if x is serialized then after deserialization value of x is 0 due to which same object of ABC may get different hashcode value
ANSWERS                                                                                                                                                     MORE