Home Java Java Interview Questions With Answers

1.which two method you need to implement for key object in HashMap?

             In order use any object as key in HashMap,it must implements equals and hashcode method in java.Read how HashMap works in java for detailed explanation on how equals and hashcode method is used to put and get object  from HashMap.

2.What is the difference between creating String as new() and literal?

When we create string with new() operator,its created in heap and not added into string pool.While string created using literal are created in string pool itself which exists in PermGen area of heap.

String s=new String(“Test”);

3.What is difference between String Buffer and String Builder in java?

String Builder in java is introduced in java 5 and only difference between both of them is that String Buffers are synchronized while String Builder is non synchronized.

4.How do you handle error condition while writing stored procedure are accessing stored procedure from java?

Stored procedure should return error code if some opertaion fails but if stored procedure itself fails than catching SqlException is only choice.

5.What is the difference between factory and abstract factory pattern?

Abstract factory provides one more level of abstraction.Consider different factories each extended from an Abstract factory.

6.What is issue with following implementation of compare To() method in java?

public int compareTo(Object o){

Employee emp=(Employee emp);

return this.id-o.id;

}

7.What are the Rules in defining a constructor?

  •  Constructor name should be same as class name.
  • It should not contain return type.
  • It should not contain Non Access Modifiers: final ,static, abstract, synchronized
  • In it logic return statement with value is not allowed.
  • It can have all four accessibility modifiers: private , public, protected, default
  • It can have parameters
  • It can have throws clause: we can throw exception from constructor.
  • It can have logic, as part of logic it can have all java legal statement except return statement with value.
  • We can not place return in constructor.

You may also like

Leave a Comment