Top 10 Core Java Interview Questions
1.What is immutable object in java?
In java any object which cannot be modified once created is called immutable object.String is an immutable class in java,once a string is created it cannot be modified.Any attempt in modification of a String results in creation of a new String object.Most of the immutable classes in java are final so that no other class can override its methods.
2. How we can make our own immutable class in java?
Here are some strategies we can apply for making a class immutable-
- First of all make the class final so that no other class can extend it and override its methods.
- Make all the fields of the class private and final.
- Do not provide setter methods which can be used to modify fields
- If there are references to the mutable objects like Date in the instance fields of class then avoid changing of those objects.
3.What is the difference between StringBuffer and StringBuilder in java?
The major difference between StringBuffer and StringBuilder class in java is that StringBuffer is thread safe in java while StringBuilder is not thread safe.StringBuilder was provided in java5
4.How HashMap works in java?
This is the mostly asked question in core java interviews from collection framework. HashMap works on the principle of hashing, HashMap stores the elements as key-value pair based on the hash code of the key in different buckets.When we need to retrive the value ,we provide the key of the k-v pair in the get() method of the HashMap which helps us in finding the right bucket with the hashcode of the key and then we can easily find the value from that bucket.
This was the simplest form of the answer but it can be explained in more detail which we are not going to discuss here.For knowing in more details refer this link-Internal Workin of HashMap
5.What are two methods we need to override for using a Object as key in HashMap?
This is also one of the important question from collection framework, and we must know if we are going to work with Map.The answer is hashcode () and equals() are the two methods which we need to override if we want to use a object as key in HashMap.But we can use String class as it is without overriding any method because these two methods are already overridden in String class.
6.What if we don't override hashcode method in java while using an object as key in HashMap?
If we don't override hashcode method in java then we will never be able to compare the objects we put as key in the hashmap and thus we will never get the value associated that object key.
7.What is the difference between creating a string as literal and with the help of new String() keyword?
If we create a String as literal then the String object will be created in in special memory named "String constant pool" memory if there is not any similar string object.And if we create a String object using new String() keyword then a new object is created in non-pool memory as usual and also a new string object in string pool memory if there is not any already.
8.What are the four principle OOPS concepts?
- 1.Abstraction
- 2.Polymorphism
- 3.Inheritence
- 4.Encapsulation
we can simply remember it as A-PIE
9.What is the difference between method overloading and method overriding?
This is also one of the most important java interview question at fresher level.Here is the answer for this question in more deatil. Difference between Method Overloading and Method overriding
10.What is super keyword in java?
super keyword is used to access the super class members.
Here it was a small list of core java interview questions which are important,there are many other things to discuss about java interviews which we will look upon in our next articles.
No comments :
Post a Comment