Java Interview Questions
1. What is Polymorphism in java?
The meaning of Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding. In java polymorphism exists in three distinct forms
Method overloading: Method overloading can be done on the basis of number of argument in a method , or order of arguments or class of the function. Overloading can’t be done on the basis of return type. This is because sometime methods are called only for their side effect and their return value might be ignored.
e.g.
public class Test {
// first menthod
void display(){}
// second menthod
void display(String msg){}
// third mothod: Not allowed
String display(){}
}
In the given example first and second display methods are overloaded on the basis of number of arguments while we can’t define third display method with same arguments and different return type as compiler won’t be able to differentiate that which method is called.
Method overriding through inheritance: Overloading methods in extended class which are defined in base class. E.g.
public class ExtendedTest extends BaseA {
// Overloaded Method
void display(String msg){}
public static void main(String[] args) {
ExtendedTest eTest = new ExtendedTest();
eTest.display("ExtendedTest");
Test test = new Test();
test.display("Test");
}
}
That means the method "display" in the "ExtendedTest" class is given more preference than " display " in the "Test" class for any " ExtendedTest " object. This is what is called overriding the methods. If there is no “display” method defined in “ExtendedTest” class then object from “ExtendedTest” will use method defined in “Test” class. That means compiler doesn’t have information at compile time that which method to call. This decision is takes at run time. That is why this is called late binding or runtime binding.
Method overriding through the Java interface
2. What do you know about Containers?
There are two types of container library available in java .
Collection: Collection category only holds one item in each location. The name is misleading, because entire container libraries are often called “collections”. Which includes List and Set. ArrayList is type of list and HashSet is type of Set.
a. List (Interface)
Order is most important feature of List
List allows insertion and deletion of element in the middle.
We can traverse List in both directions.
ArrayList:
ArrayList allows random access of elements.
It is slow when inserting and removing elements from the middle
LinkedList :
Provides optimal sequential access.
Inexpensive insertions and deletions from the middle of the List.
Relatively slow for sequential access.
b. Set (Interface)
Element in set are unique.
Set doesn’t guarantee that it will maintain any order.
HashSet:
Provides fast lookup
Map: The Map holds key-value pairs, rather like a mini database. We can lookup on the basis of key.
a. HashMap:
Implementation based on Hash table.
Provides constant time performance for inserting and location pairs.
3. Difference between HashTable and HashMap?
The HashTable is almost equal to HashMap except following points.
1. HashTable is synchronized, while HashMap is not.
2. HashTable doesn’t permit null as key while in HashMap it is allowed.
3. HashTable guarantee that order of the table will remain constant over time while in HashMap it is not.
4. What are the differences between Vector and ArrayList?
Vector is synchronized. Content in vector is thread-safe. ArrayList is not synchronized hence its content is not thread-safe. Synchronization is not at free of cost. It affects vector’s performance. So if we don’t require thread-safe data, we should use ArrayList.
Internally, both the ArrayList and vector hold data in Array. When we add a new element into ArrayList or Vector, and the object will need to expand its internal array if it runs out of memory. Vector doubles the size of Array by default, while the ArrayList increases its size by 50 percent. Vector does have slight advantage since they allow to reset increment size.
Addition and deletion operation in both classes takes same time. Addition at the end of container can be performed in constant time O(1). Deletion and Addition are more expansive when the element is added/removed from middle, it takes O(n-i). Here n is number of elements in the container and i is index of the element.
No comments:
Post a Comment