1) Can we use restful webservice along with spring MVC directly?
Yes we have to use spring boot. For example:
@RestController
@RequestMapping("/tct-pw/admin")
public class UserRoleController {
private static final Logger logger = LoggerFactory.getLogger(UserRoleController.class);
@Autowired
UserRoleService usrRoleService;
@RequestMapping(value="/getuserrole",method = RequestMethod.GET)
public @ResponseBody
Map<String,Map<String,List<String>>> getUserRoleList(@RequestParam("sso") String sso) {
List<String> lstAvailableRoles = new ArrayList<String>();
List<String> lstAssignedRoles = new ArrayList<String>();
Map<String,List<String>> mpRoles = new HashMap<String,List<String>>();
Map<String,Map<String,List<String>>> result = new HashMap<String,Map<String,List<String>>>();
lstAvailableRoles = usrRoleService.getAvailableRoleList(sso);
lstAssignedRoles = usrRoleService.getAssignedRoleList(sso);
mpRoles.put(AppConstant.AVAILABLE_ROLE, lstAvailableRoles);
mpRoles.put(AppConstant.ASSIGNED_ROLE, lstAssignedRoles);
result.put(AppConstant.RESULTS,mpRoles);
return result;
}
3) Exception Example:
Will it be compiled:
package p1;
import java.io.IOException;
public class Sample
{
public void methodA() throws IOException
{
}
}
class B extends Sample
{
public void methodA() throws Exception
{
}
}
Ans: no, it will not be compiled. Because up casting is not allowed here, but vice versa is fine.
In case of unchecked exception it is allowed. If we ignore throws in subclass then also it is fine.
5) What are generics, have you worked on it, have you created your own generics. What is the benefit of generics?
Ans:
Generic method example:
public class GenericMethodTest {
// generic method printArray
public static <E> void printArray(E[] inputArray) {
// Display array elements
for (E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
public static void main(String args[]) {
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println("Array integerArray contains:");
printMyArray(intArray); // pass an Integer array
System.out.println("\nArray doubleArray contains:");
printMyArray(doubleArray); // pass a Double array
System.out.println("\nArray characterArray contains:");
printMyArray(charArray); // pass a Character array
}
public static <MY> void printMyArray(MY[] m)
{
for(MY e:m)
{
System.out.print(e + " ");
}
System.out.println();
}
}
Bounded type parameter example:
public class MaximumTest
{
// determines the largest of three Comparable objects
public static <T extends Comparable<T>> T maximum(T x, T y, T z)
{
T max = x; // assume x is initially the largest
if ( y.compareTo( max ) > 0 ){
max = y; // y is the largest so far
}
if ( z.compareTo( max ) > 0 ){
max = z; // z is the largest now
}
return max; // returns the largest object
}
public static void main( String args[] )
{
System.out.printf( "Max of %d, %d and %d is %d\n\n",
3, 4, 5, maximum( 3, 4, 5 ) );
System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1f\n\n",
6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
System.out.printf( "Max of %s, %s and %s is %s\n","pear",
"apple", "orange", maximum( "pear", "apple", "orange" ) );
}
}
Generic class example:
public class Box<T> {
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<String> stringBox = new Box<String>();
integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));
System.out.printf("Integer Value :%d\n\n", integerBox.get());
System.out.printf("String Value :%s\n", stringBox.get());
}
}
6) What is the difference between ConcurrentHashMap and HashMap?
ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java, which is a synchronized collection class, that makes the main difference between HashMap and ConcurrentHashMapwhich is one is non synchronized , non thread safe and not for use in Concurrent multi-threaded environment while ...
ConcurrentHashMap
· You should use ConcurrentHashMap when you need very high concurrency in your project.
· It is thread safe without synchronizing the whole map.
- Reads can happen very fast while write is done with a lock.
- There is no locking at the object level.
- The locking is at a much finer granularity at a hashmap bucket level.
- ConcurrentHashMap doesn’t throw a ConcurrentModificationException if one thread tries to modify it while another is iterating over it.
- ConcurrentHashMap uses multitude of locks.
SynchronizedHashMap
· Synchronization at Object level.
· Every read/write operation needs to acquire lock.
· Locking the entire collection is a performance overhead.
· This essentially gives access to only one thread to the entire map & blocks all the other threads.
· It may cause contention.
· SynchronizedHashMap returns Iterator, which fails-fast on concurrent modification.
9) What is the benefit of using annotations, have you created your own annotations. How it works internally.
Ans: Benefits:
Annotations have a lot of advantages over XML, to name a few :
· Static type checking - the compiler will check for you where the annotation (once defined properly) is applicable and how
· Clean code - its much easier to see (visually) the meta data defined in annotations
Disadvantage:
· XML doesn't require recompilation when you want to change something. With annotation you'll have to recompile.
Refer page no 4 in HCL questions answer.
10) What are all annotations you have used in restful webservice.
Ans:
@ApplicationPath
annotation is used to define the base URI of the resource in rest implementation.
@ApplicationPath("/rest") – works as an application path (root) for rest ws.
@RequestScoped: An object which is defined as @RequestScoped is created once for every request and is shared by all the bean that inject it throughout a request.
@Path – The @Path annotation identifies the URI path template to which the resource responds, and is specified at the class level of a resource.
@Path("/users/{username}")
In this type of example, a user will be prompted to enter their name, and then a Jersey web service configured to respond to requests to this URI path template will respond. For example, if the user entered their user name as Galileo, the web service will respond to the following URL:
http://example.com/users/Galileo
To obtain the value of the username variable, the @PathParamannotation may be used on the method parameter of a request method, as shown in the following code example.
@Path("/users/{username}")
public class UserResource {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
...
}
}
@POST
@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. If@Consumes is applied at the class level, all the response methods accept the specified MIME types by default. If @Consumes is applied at the method level, it overrides any @Consumes annotations applied at the class level.
If a resource is unable to consume the MIME type of a client request, the Jersey runtime sends back an HTTP “415 Unsupported Media Type” error.
The value of @Consumes is an array of String of acceptable MIME types. For example:
@Consumes({"text/plain,text/html"})
The following example shows how to apply @Consumes at both the class and method levels:
@Path("/myResource")
@Consumes("multipart/related")
public class SomeResource {
@POST
public String doPost(MimeMultipart mimeMultipartData) {
...
}
@POST
@Consumes("application/x-www-form-urlencoded")
public String doPost2(FormURLEncodedProperties formData) {
...
}
}
The doPost method defaults to the MIME media type of the @Consumes annotation at the class level. The doPost2 method overrides the class level @Consumes annotation to specify that it can accept URL-encoded form data.
If no resource methods can respond to the requested MIME type, an HTTP 415 error (Unsupported Media Type) is returned to the client.
The HelloWorld example discussed previously in this section can be modified to set the cliched message using @Consumes, as shown in the following code example.
@POST
@Consumes("text/plain")
public void postClichedMessage(String message) {
// Store the message
}
In this example, the Java method will consume representations identified by the MIME media type text/plain. Notice that the resource method returns void. This means no representation is returned and response with a status code of HTTP 204 (No Content) will be returned.
@FormParam("suppCodeDown") String suppCode
@QueryParam("suppCodeDown") String suppCode
@PathParam("username") String username
@Inject
16) How HashMaps works internally.What is Hashing?
Hashing in its simplest form, is a way to assigning a unique code for any variable/object after applying any formula/algorithm on its properties. A true Hashing function must follow this rule:
Hash function should return the same hash code each and every time, when function is applied on same or equal objects. In other words, two equal objects must produce same hash code consistently.
Note: All objects in java inherit a default implementation of hashCode() function defined in Object class. This function produce hash code by typically converting the internal address of the object into an integer, thus producing different hash codes for all different objects.
HashMap is an array of Entry objects:
Consider HashMap as just an array of objects.
Have a look what this Object is:
1. static class Entry<K,V> implements Map.Entry<K,V> {
2. final K key;
3. V value;
4. Entry<K,V> next;
5. final int hash;
6. ...
7. }
Each Entry object represents key-value pair. Field next refers to other Entry object if a bucket has more than 1 Entry.
Sometimes it might happen that hashCodes for 2 different objects are the same. In this case 2 objects will be saved in one bucket and will be presented as LinkedList. The entry point is more recently added object. This object refers to other object with next field and so one. Last entry refers to null.
When you create HashMap with default constructor
1. HashMap hashMap = new HashMap();
Array is gets created with size 16 and default 0.75 load balance.
Adding a new key-value pair
1. Calculate hashcode for the key
2. Calculate position hash % (arrayLength-1))
where element should be placed(bucket number)
3. If you try to add a value with a key which has already been saved in HashMap, then value gets overwritten.
4. Otherwise element is added to the bucket. If bucket has already at least one element - a new one is gets added and placed in the first position in the bucket. Its next
field refers to the old element.
Deletion:
1. Calculate hashcode for the given key
2. Calculate bucket number (hash % (arrayLength-1))
3. Get a reference to the first Entry object in the bucket and by means of equals method iterate over all entries in the given bucket. Eventually we will find correct Entry. If desired element is not found - return null
What put() method actually does:
Before going into put() method’s implementation, it is very important to learn that instances of Entry class are stored in an array.HashMap class defines this variable as:
1.
2.
3.
4. transient Entry[] table;
Now look at code implementation of put() method:
1.
2.
3.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15. public V put(K key, V value) {
16. if (key == null)
17. return putForNullKey(value);
18. int hash = hash(key.hashCode());
19. int i = indexFor(hash, table.length);
20. for (Entry<k , V> e = table[i]; e != null; e = e.next) {
21. Object k;
22. if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
23. V oldValue = e.value;
24. e.value = value;
25. e.recordAccess(this);
26. return oldValue;
27. }
28. }
29.
30. modCount++;
31. addEntry(hash, key, value, i);
32. return null;
33. }
Lets note down the steps one by one:
Step1- First of all, key object is checked for null. If key is null, value is stored in table[0] position. Because hash code for null is always 0.
Step2- Then on next step, a hash value is calculated using key’s hash code by calling its hashCode() method. This hash value is used to calculate index in array for storing Entry object. JDK designers well assumed that there might be some poorly writtenhashCode() functions that can return very high or low hash code value. To solve this issue, they introduced another hash() function, and passed the object’s hash code to this hash() function to bring hash value in range of array index size.
Step3- Now indexFor(hash, table.length) function is called to calculate exact index position for storing the Entry object.
Step4- Here comes the main part. Now, as we know that two unequal objects can have same hash code value, how two different objects will be stored in same array location [called bucket].
Answer is LinkedList. If you remember, Entry class had an attribute “next”. This attribute always points to next object in chain. This is exactly the behavior of LinkedList.
So, in case of collision, Entry objects are stored in LinkedList form. When an Entry object needs to be stored in particular index, HashMap checks whether there is already an entry?? If there is no entry already present, Entry object is stored in this location.
If there is already an object sitting on calculated index, its next attribute is checked. If it is null, and current Entry object becomes next node in LinkedList. If next variable is not null, procedure is followed until next is evaluated as null.
What if we add the another value object with same key as entered before. Logically, it should replace the old value. How it is done? Well, after determining the index position of Entry object, while iterating over LinkedList on calculated index, HashMap calls equals method on key object for each Entry object. All these Entry objects in LinkedList will have similar hash code but equals() method will test for true equality. If key.equals(k) will be true then both keys are treated as same key object. This will cause the replacing of value object inside Entry object only.
In this way, HashMap ensure the uniqueness of keys.How get() methods works internally
Now we have got the idea, how key-value pairs are stored in HashMap. Next big question is : what happens when an object is passed in get method of HashMap? How the value object is determined?
Answer we already should know that the way key uniqueness is determined in put() method , same logic is applied in get() method also. The moment HashMap identify exact match for the key object passed as argument, it simply returns the value object stored in current Entry object.
If no match is found, get() method returns null.
Let have a look at code:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19. public V get(Object key) {
20. if (key == null)
21. return getForNullKey();
22. int hash = hash(key.hashCode());
23. for (Entry<k , V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
24. Object k;
25. if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
26. return e.value;
27. }
28. return null;
29. }
Q1. How HashSet implements hashing?
A. Method internally uses HashMap’s hash method for hasihng.
Q2. How add method works internally?
A. public void add(E value){
hashMapCustom.put(value, null);
}
Method internally uses HashMap’s put method for storing object.
Q3. How contains method works internally?
A. public boolean contains(E obj){
return hashMapCustom.contains(obj) !=null ? true :false;
}
Method internally uses HashMap’s contains method for storing object.
Q4. How remove method works internally?
A. public boolean remove(E obj){
return hashMapCustom.remove(obj);
}
Method internally uses HashMap’s put remove for storing object.
11) What is jersey weservice, how do you implement it.
12) Have you worked on JMS?
Ans: Yes, refer HCL interview document.
13) Have you used assertion?
14) enumeration example?
15) How @Inject works internally.
7) What was other features introduced in java 5?
Ans:
8) What is architecture of your project?
Ans:
4) Have you worked with Stack and Queue data structure in java? What is blocking queue, What is Type…