1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

Java interview questions

  • How to retrieve a key and Value from a map ? view answer
  • Why can an interface supertype call methods that belong to Object class? view answer
  • Q. What is difference between shallow copy and deep copy ? view answer
  • Q.Why to override equals() and hashCode()? and How i can implement both equals() and hashCode() for Set ? view answer
  • Q.How to sort list of objects ( User Defined) using comparator? in Descending Order. view answer
  • Q. How to sort list of objects ( User Defined) using comparator? view answer
  • What is the difference between int and Interger? view answer
  • Singleton Double-checked locking in Java? view answer
  • What are the parameters to follow Creating and Destroying Objects in Java? view answer
  • Q.What are the different scopes for Java variables? view answer
  • Q.What method must be implemented by all threads? view answer
  • Q.Can an unreachable object become reachable again? view answer
  • Q.What are the steps in the JDBC connection? view answer
  • Q.How are this() and super() used with constructors? view answer
  • Q.What is the difference between static and non-static variables? view answer
  • Q.What is the purpose of finalization? view answer
  • Q.Does garbage collection guarantee that a program will not run out of memory? view answer
  • Q.What is synchronization and why is it important? view answer
  • Q.What is daemon thread and which method is used to create the daemon thread? view answer
  • Q.What are synchronized methods and synchronized statements? view answer
  • Q.If I write System.exit (0); at the end of the try block, will the finally block still execute? view answer
  • Q.If I write return at the end of the try block, will the finally block still execute? view answer
  • Q.Is it necessary that each try block must be followed by a catch block? view answer
  • Q.What is the basic difference between the 2 approaches to exception handling. 1> try catch block and 2> specifying the candidate exceptions in the throws clause? When should you use which approach? view answer
  • Q.What are the different ways to handle exceptions? view answer
  • Q.What is the difference between error and an exception? view answer
  • Q: What is wrapper class? Explain with example? view answer
  • Q.What is serialization? Explain with example? view answer
  • Q.What one should take care of while serializing the object? view answer
  • Q. When you serialize an object, what happens to the object references included in the object? view answer
  • Q.What happens to the static fields of a class during serialization? view answer
  • Q. Objects are passed by value or by reference? view answer
  • Q. Primitive data types are passed by reference or pass by value? view answer
  • Q. What type of parameter passing does Java support? view answer
  • Q.Can a top level class be private or protected? view answer
  • Q.What is the default value of an object reference declared as an instance variable? view answer
  • Q.What is the difference between declaring a variable and defining a variable? view answer
  • Q. What are different types of inner classes? view answer
  • Q. What is Overriding? view answer
  • Q.What are Checked and UnChecked Exception? view answer
  • Q. What is final? view answer
  • Q.What is static in java? view answer
  • What is an abstract class? view answer
  • Q. What are the modifiers in Java ? view answer
  • Q.Difference between HashMap and HashTable? view answer
  • Q.Difference between Vector and ArrayList? view answer
  • Q.Difference between ArrayList and LinkedList? view answer
  • Q.What are pass by reference and passby value in Java? view answer
  • Q. When we go for Abstract and Interface in Java? view answer
  • Q.What is the difference between interface and abstract class? view answer
  • Q.What are the Garbage collection algorithms in Java? view answer
  • Q.What is garbage collection and the purpose of garbage collection in Java? view answer
  • Q. What is the difference between an Interface and an Abstract class? view answer

!!! Java interview questions !!!

Q.Why to override equals() and hashCode()? and How i can implement both equals() and hashCode() for Set ?



If you are implementing HashSet to store unique object then you need to implement equals() and hashcode() method.

if two objects are equal according to the equals() method, they must have the same hashCode() value (although the reverse is not generally true).

Two scenarios

Case 1) : If you don't implement equals() and hashcode() method :

When you are adding objects to HashSet , HashSet checks for uniqueness using
equals() and hashcode() method the class ( ex. Emp class). If there is no equals() and hashcode() method the Emp class then HashSet checks default Object classes
equals() and hashcode() method.

In the Object class , equals method is
public boolean equals(Object obj) {
    return (this == obj);
}
Under this default implementation, two references are equal only if they refer to the exact same object. Similarly, the default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value.

This will fail to check if two Emp object with same employee name .
For Example :
Emp emp1 = new Emp();
emp1.setName("sat");
Emp emp2 = new Emp();
emp2.setName("sat");
Both the objects are same but based on the default above method both objects are dirrefent because references and hashcode are different .
So in the HashSet you can find the duplicate emp objects.

To overcome the issue equals() and hashcode() method need to override.
Case 2) : If you override equals() and hashcode() method

Example : implement equals and hashcode
public class Emp {
    private long empId;
    String name = "";
public boolean equals(Object o) {
        if (o == this)
        return true;
        if (!(o instanceof Emp))
        return false;
        Emp emp = (Emp)o;
        return emp. empId == empId &&
        emp. name == name;
        
        }
    
    public int hashCode(){
        int result = 10;
        result = result * new Integer(String.valueOf(empId)).intValue();
        return result;
    }
}
In the equals() , it check for name is same or not. This way you can find out the objects are equals or not.
In the hashCode() also it return some unique value for each object. In this way if two Emp object has same empId then it will say both are same object.
Now HashSet store only unique objects.
If you do
Emp emp1 = new Emp();
emp1.setName("sat");
Emp emp2 = new Emp();
emp2.setName("sat");

if(emp1.equals(emp2)){
            System.out.println("equal");
        }
This will print : equal

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy