Showing posts with label Advanced. Show all posts
Showing posts with label Advanced. Show all posts

Spring MVC Framework Collection API Question - 2

This is the second set of questions that are generally asked for Collection Framework questions. These are a bit more advanced then the earlier set of questions.

Question 1: What is difference between ArrayList and LinkedList?
There are quiet a few difference between ArrayList and LinkedList:
  1. ArrayList uses a primitive array to store the values internally. However, LinkedList is made up of nodes where each nodes has three elements: value, pointer to next element and pointer to previous element.
  2. ArrayList implements the RandomAccess interface whereas LinkedList does not. RandomAccess is a marker interface which states that the implementation implements the fast algorithm to access the Nth element. So accessing Nth element is fast and takes constant-time for ArrayList whereas LinkedList has to scan the whole list to find the Nth element, hence making it slower for random access.
  3. Inserting and deleting an element at the start and end is faster in LinkedList as compared to ArrayList. While inserting an element in the LinkedList at the start and the beginning , we just need to create a node and assign the pointers whereas in ArrayList if you are inserting an element at the start, then first it will copy all the elements in a different list and then adds them after it, similarly while adding an element at the end means ArrayList will have to scan the whole list for inserting an element.
  4. LinkedList usually takes more memory than ArrayList as each node in the LinkedList will also store the next and previous pointers.
  5. ArrayList may also have issue if your list fills up quiet frequently as the ArrayList will create a new list with increased capacity and then copies all the elements whereas no such thing is required for LinkedList.
Question 2: Where will you use ArrayList and where will you use LinkedList?
If you have add elements at the start and end frequently or iterate over it for deleting elements then you should consider using LinkedList as it requires constant time for these operations and linear-time in ArrayList. If you have to access the positional elements then you should consider using ArrayList as it takes constant time for that and LinkedList take linear time for positional access.

Question 3: What is the difference between HashMap and HashTable?
The main difference is that HashTable is synchronised whereas HashMap is not. You need to provide external synchronisation if you want to synchronise the HashMap. Another difference is that HashTable does not allows null as keys or values whereas HashMap allow one null key and any number of null values.

Question 4: What is the difference between HashMap and TreeMap?
The basic difference is that in TreeMap objects are stored in an order decided either by the natural ordering of key or by the comparator that is defined at the the instantiation time of the TreeMap whereas HashMap does not guarantee any ordering. Since the elements are stored in an order, I think it is safe to say that the insertion of element in TreeMap will be slower as compared to HashMap whereas retrieval will be faster than HashMap.

Question 5: Explain how HashMap works or how the hashcode() and equals() method is used by the HashMap or how the get() method works in HashMap?
This question generally is the starting point for more complicated questions on HashMap. Basically what happens is when we call get(), put() method of HashMap. The HashMap uses the hashcode() method of the key to find the hashcode, then it uses its internal hashing mechanism to find the index of the correct "bucket" where the value might be stored. This bucket contains a list of Map.Entry objects in the form of a linked list. Once the bucket is identified, the map will traverse through the Map.Entry to find the exact key by using equals method, once found it will overwrite or return the value.

Question 6: How is HashSet implemented? or How will you implement the HashSet using HashMap?
Actually if you look closely on the HashMap methods there is already a set in the HashMap, they keySet. It has all the properties like no duplicates (HashMap does not allow duplicate keys). So, all you need to do is following, when you insert an object in HashSet, you insert the object as key in the HashMap and put the value as an EMPTY object. Same is the case when retrieving a value from HashSet, rather returning a value just return the key.

Question 7: Is Collection.synchronisedMap() is really thread-safe?
Question 8: What is the difference between ArrayList and Vector?
As per the Java API, the main difference between the ArrayList and Vector is that Vector is synchronised whereas ArrayList is not. The other difference is the way there size is incremented, Vector always doubles the size whereas ArrayList increases the size by half the initial capacity. So if there is a need for thread-safety is advisable to use Vector but since synchronisation takes a hit on the performance we may consider using the ArrayList and synchronise it using the Collections utility class.

Question 9: What are the mandatory  methods you should override while using TreeMap and why?

Spring MVC Framework Spring Interview Question - 2

This is the second set of questions that are generally asked during the interviews on Spring Framework. Some of these are advanced level questions like on the MVC architecture.

Question 1: What is BeanFactory?
Bean Factory is a factory class that contains the definition of multiple beans in itself, it can instantiate various beans when asked by the client. It has the capabilities to perform two major task during this process.
  • It is able to resolve the association between collaborating objects as the beans are instantiated. It in term removes the burden of the the configuration from the bean itself or from the calling client.
  • It can also call the various life cycle methods of beans and can calls the instantiation and destruction methods.
Question 2: What is ApplicationContext?
ApplicationContext is also like BeanFactory, the difference being that BeanFactory can only be created programmatically  whereas ApplciationContext can be created declaratively using ContextLoaders, for example a ContextLoaderListener. Apart from being able to perform the lifecycle control just like BeanFactory it can also load the various text message resources, file resources and can also pass the various events to beans that are registered as Listeners.

Question 3: What is Dependency Injection and Why is it becoming the de-facto standards for applications?
Dependency injection is the mechanism provided by framework where in the framework is responsible for creating the various dependencies and injecting them rather than the object creating the dependency. It becoming quite popular these days for the simple reason that it provides a loose coupling between various components. So, tomorrow if one of the dependency  changes rather then the whole application getting effect all the programmers need to do is just change the configuration as long as the changed bean also follows the same "contract" / interface as the previous one.

Question 4: What is the Spring's MVC architecture? or How does a web Request flows through Spring MVC?
Spring MVC architecture consists of the following things:
  • DispatcherServlet
  • LocaleResolver
  • MultipartResolver
  • ThemeResolver
  • HandlerMappings
  • Controller
  • ResultToViewNameTranslator
  • ViewResolver
The above components works as following to serve the request.
  • The DispatcherServlet receives the request from client
  • It uses the LocaleResolver to load any properties based on the Locale of the generated request.
  • It then uses the ThemeResolver to find any specific theme / template that might be used for that particular locale.
  • Then it find out whether the request has a multipart mime type then it initialises the file upload classes.
  • Then it uses the HandlerMappings to find the correct controller, where it uses the Model / Service Layer to execute the business logic. Before calling the controller method it has the capability to call the Pre adn Post Controller interceptor, which largely work as filters. 
  • Then it calls the ResultToViewNameTranslator to find the correct view.
  • Then the controller populates the Model and uses the ViewResolver to find the next view. It then combines the Model and the View to prepare the view, which is then passed to the container to return as Response.
Question 5: What are the main class in the Spring MVC architecture?
The main classes in the MVC architecture are as follows:
  1. DispatcherServlet
  2. MultipartResolver
  3. LocaleResolver
  4. ThemeResolver
  5. HandlerMapping
  6. HandlerAdapters
  7. HandlerExceptionResolver
  8. ResultToViewNameTranslator
  9. ViewResolver
Question 6: What is the typical lifecycle of a bean in the bean factory container?
Bean lifecycle in a bean container is as follows:
  • BeacFactory reads the bean definition from the xml configuration file.
  • BeanFactory instantiate the bean.
  • Using the dependency injection bean factory populates all the bean properties as specified in the bean definition.
  • If the bean implements BeanNameAware interface, then it calls the setBeanName() method passing the bean id.
  • If the bean implements BeanFactoryAware interface, then it calls the setBeanFactory() method passing an instance of itself.
  • If the bean has any BeanPostProcessors associated with itself it will call the postProcessBeforeTheInitialization() method.
  • If any init method is declare it will call that method and initialize the bean.
  • Finally if any BeanPostProcessor is associated with the bean then it will call the postProcessAfterInitialization().
Question 7: What are various Bean scopes available in Spring?
Spring supports the following scopes:
  • singleton - This is the default scope of a spring bean, it means the same instance is available through out the application
  • prototype - This means that the bean act as a template and a new instance will be injected for different beans.
  • request - The bean definition is available till the scope of an HTTP request. This is only applicable in case of the web-aware application context.
  • session - The bean is available till the scope of HTTP session. This is only applicable in case of the web-aware application context.
  • global-session - The bean is available till the scope of a global HTTP session. This is typically applicable in case of portlet context.
Question 8: Why is Spring MVC better than Struts ?