This is a general blog where we post the most commonly asked Interview questions in the companies.
HTTP Request | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
The request sent by the computer to a web server, contains all sorts of potentially interesting information; it is known as HTTP requests. The HTTP client sends the request to the server in the form of request message which includes following information:
The HTTP request method indicates the method to be performed on the resource identified by the Requested URI (Uniform Resource Identifier). This method is case-sensitive and should be used in uppercase. The HTTP request methods are:
|
There are many differences between the Get and Post request. Let's see these differences:
GET | POST |
---|---|
1) In case of Get request, only limited amount of data can be sent because data is sent in header. | In case of post request, large amount of data can be sent because data is sent in body. |
2) Get request is not secured because data is exposed in URL bar. | Post request is secured because data is not exposed in URL bar. |
3) Get request can be bookmarked. | Post request cannot be bookmarked. |
4) Get request is idempotent . It means second request will be ignored until response of first request is delivered | Post request is non-idempotent. |
5) Get request is more efficient and used more than Post. | Post request is less efficient and used less than get. |
Some other features of GET requests are:
Some other features of POST requests are:
Why String is immutable in Java? |
---|
String is immutable in java. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information cannot be modified. Following are the advantages of String being immutable:
|
When Collision occurs in Hashmap and what are the resolution techniques? |
---|
A collision will occur on Hashtable or HashMap when hashCode() method of two different key objects will return same values. That's all about how HashMap in Java handles collisions. In general, this method is called chaining because all objects stored in the same bucket are chained as linked list Prior to Java 8, HashMap and all other hash table based Map implementation classes in Java handle collision by chaining, i.e. they use linked list to store map entries which ended in the same bucket due to a collision. If a key end up in same bucket location where an entry is already stored then this entry is just added at the head of the linked list there. In the worst case this degrades the performance of the get() method of HashMap to O(n) from O(1). In order to address this issue in the case of frequent HashMap collisions, Java8 has started using a balanced tree instead of linked list for storing collided entries. This also means that in the worst case you will get a performance boost from O(n) to O(log n). The threshold of switching to the balanced tree is defined as TREEIFY_THRESHOLD constant in java.util.HashMap JDK 8 code. Currently, it's value is 8, which means if there are more than 8 elements in the same bucket than HashMap will use a tree instead of linked list to hold them in the same bucket. This feature will not available to all hash table based classes in Java e.g. Hashtable will not have this feature because of its legacy nature and given that this feature can change the traditional legacy iteration order of Hashtable. Similarly, WeakHashMap will also not include this feature. 2) From Java 8 onwards, HashMap, ConcurrentHashMap, and LinkedHashMap will use the balanced tree in place of linked list to handle frequently hash collisions. The idea is to switch to the balanced tree once the number of items in a hash bucket grows beyond a certain threshold. This will improve the worst case get() method performance from O(n) to O(log n). 3) By switching from linked list to balanced tree for handling collision, the iteration order of HashMap will change. This is Ok because HashMap doesn't provide any guarantee on iteration order and any code which depends upon that are likely to break. 4) Legacy class Hashtable which exists in JDK from Java 1 will not use the balanced binary tree to handle frequent hash collision to keep its iteration order intact. This was decided to avoid breaking many legacy Java application which depends upon iteration order of Hashtable. 5) Apart from Hashtable, WeakHashMap and IdentityHashMap will also continue to use the linked list for handling collision even in the case of frequent collisions. 6) Collision in HashMap is possible because hash function uses hashCode() of key object and equals() and hashCode() contract doesn't guarantee different hashCode for different objects. Remember, they guarantee same hash code for the equal object but not the vice-versa. 7) A collision will occur on Hashtable or HashMap when hashCode() method of two different key objects will return same values. |