This article discusses frequently asked Java Interview Questions and Answers.
Common Java Interview Questions and Answers :
- What is NullPointerException when there are no pointers in Java ?
- Why should we Override equals() method ?
- Explain equals() and hashcode() method significance in Java.
- How hashing retrieval works using hashcode() and equals() ?
- How to implement hashcode() method in Java ?
- Why return type of hashcode() is int and not long ?
- What are the basic Collection flavors ?
- What is the difference between Sorted and Ordered Collections ?
- What is String immutability ?
- What is String Constant Pool ?
- What is the difference between creating a String literal and String object using new String() ?
- What is Serialization ?
- Explain how Serialization works with Inheritance
- Can we serialize Static variables ?
- Common questions regarding following special properties of Interfaces
This article discusses the first 15 of the Java Interview questions and answers. Please refer the following articles for subsequent Java Interview Questions and Answers:
Java Interview Questions – Part 2
Java Interview Questions – Part 3
Java Interview Questions – Part 4
Java Interview Question 1.
What is NullPointerException when there are no pointers in Java?
When you declare a reference variable (i.e. an object) you are really creating a pointer to an object.
Integer value;
Here, we declare a variable named value, and it contains a pointer (because the type is Integer which is a reference type). Since we did not specify what is should point to, it currently points to nothing.
Integer value = new Integer(10);
Here, the new keyword instantiates an Integer and the pointer variable “value” is assigned it. You can now reference the object using the dereferencing operator “.” (dot).
In first scenario, you declare a variable but did not create an object. If you attempt to dereference the variable before creating the object you get a NullPointerException.
Java Interview Question 2.
Why should we Override equals() method ?
The equals() method in class Object uses only the == operator for comparisons, So unless you override equals(), two objects would be considered equal only if the two references refer to the same object.
If you want objects of your class as keys for a hashtable or hashmap, then you must override equals() so that two different instances can be considered same depending on the values they store.
Java Interview Question 3.
Explain equals() and hashcode() method significance in Java.
When you override equals(), two objects would be considered equal depending on the values they store and not only depending on the references.
If two objects are considered equal using the equals() method, then they must have identical hashcode values.
Hashcodes are typically used to increase the performance of large collections of data.
Java Interview Question 4.
How hashing retrieval works using hashcode() and equals() ?
- hashcode() is used to find the right bucket when looking in a hashmap
- If the bucket has multiple elements, then equals() is used to search the bucket for the right element.
Java Interview Question 5.
How to implement hashcode() method ?
Normally, hashcode() implementations do some combinations of XOR-ing on the class’s instance variables or multiply them with some prime numbers.
But even the one-hash-fits-all method shown below would be allowed, though not appropriate.
Public int hashcode(){return 1234;}
Here is an implementation using prime numbers :
@Override public int hashCode() { int hash = 7; hash = 31 * hash + (null == itemId ? 0 : itemId.hashCode()); return hash; }
Here is another approach if an object has 2 fields a and b :
@Override public int hashCode() { return ((a+b).hashCode()); }
Java 7 onward, we can also use Objects.hash() for hashcode() implementation.
For example, if an object that has 2 fields, a and b, we could override hashcode() as :
@Override public int hashCode() { return Objects.hash(a, b); }
N.B. Don’t use transient variables in hashcode() implementation since they would be defaulted to initial value on de-serialization.
Java Interview Question 6.
Why return type of hashcode() is int and not long ?
The maximum length of an array is Integer.MAX_VALUE
Since the primary use of hashcode() is to determine in which slot to insert an object into the backing array of hashtable/hashmap, a hashcode greater than Integer.MAX_VALUE can not be stored in an array
Java Interview Question 7.
What are the basic Collection flavors ?
List – List of things
Set – Unique things
Map – Things with Unique ID
Queue – Things arranged by the order in which they are to be processed
Java Interview Question 8.
What is the difference between Sorted and Ordered Collections ?
When a collections is ordered, it means you can iterate through the collection in a specific order,such as insertion order.
A Hashtable/HashMap collection is not ordered, where as an ArrayList is ordered by elements index position and LinkedHashMap is ordered by the insertion order.
Some collections keep an order referred as natural order of elements.. those collections are not just ordered, they are sorted. The natural order is specified the Comparable interface. But it is also possible to define other sort orders using Comparator interface
Java Interview Question 9.
What is String immutability ?
String class is immutable. String class is final so no one can modify behavior of any of the string methods. So, no body can override the String methods to alter the immutability.
StringBuffer is mutable but synchronized however StringBuilder is not synchronized.
Java Interview Question 10.
What is String Constant Pool ?
For memory efficiency, JVM sets aside a special area of memory called “String constant pool”. When the compiler encounters a string literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String and no new String literal object is created.
Java Interview Question 11.
What is the difference between creating a String literal and a String object using new String() ?
Following statement creates one String object and one reference variable
String s = “abc”;
new String() creates two String objects and one reference.
String s = new String(“abc”);
This is because, when new keyword is used, a new object is created in non-pool memory and “s” refers to it and also literal “abc” will be put in the pool.
Java Interview Question 12.
What is Serialization ?
When you serialize an object, Java serialization takes care of saving that object’s entire object graph. This means a deep copy of everything the saved object needs to be restored.
Basic serialization happens with just two methods: one to serialize objects and write to a stream and a second to read the stream and deserialize objects.
ObjectOutputStream writeObject() // serialize and write
ObjectInputStream readObject() // read and deserialize
Java Interview Question 13.
Explain how Serialization works with Inheritance
If a superclass is Serializable, all subclasses are also Serializable.
If the superclass is not serializable, then any instance variable you inherit from superclass will be reset to the original values given during construction since the constructors of non-serializable class would run.
Java Interview Question 14.
Can we serialize Static variables ?
Static variables are not part of Object’s state and hence are not serialized. This is because Serialization applies only to objects.
Java Interview Question 15.
Common questions regarding following special properties of Interfaces(Pre Java 8)
- All variables defined in an interface must be public, static and final. In other words, interfaces can declare only constants, not instance variables.
- All methods are implicitly public and abstract
- Interface methods must not be static
- Interface methods can not be final
- An interface can extend one or more interfaces
- An interface can not extend anything, but another interface
- An interface can not implement another class or interface
You may also like the following posts :
Object Oriented Programming Interview Questions and Answers
Java 8 Interview Questions and Answers
Coding Interview Questions in Java
© 2015 – 2023, https:. All rights reserved. On republishing this post, you must provide link to original post