Interview Questions and Answers related to Architecture / Design Choices and Decisions

Q1.  If you are given a choice to use either ArrayList and LinkedList, Which one would you use and Why ?

Ans. ArrayList are implemented in memory as arrays and hence allows fast retrieval through indices but are costly if new elements are to be inserted in between other elements. 

LinkedList allows for constant-time insertions or removals using iterators, but only sequential access of elements 

1. Retrieval - If Elements are to be retrieved sequentially only, Linked List is preferred.
2. Insertion - If new Elements are to be inserted in between other elements , Array List is preferred.
3. Search - Binary Search and other optimized way of searching is not possible on Linked List.
4. Sorting - Initial sorting could be pain but lateral addition of elements in a sorted list is good with linked list.
5. Adding Elements - If sufficiently large elements needs to be added very frequently ,Linked List is preferable as elements don't need consecutive memory location.

Q2.  Can you provide some implementation of a Dictionary having large number of words ? 

Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.

Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.

Further level up, we can have linked Hashmaps like ,

hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)

upto n levels ( where n is the average size of the word in dictionary.

Q3.  Why using cookie to store session info is a better idea than just using session info in the request ?

Ans. Session info in the request can be intercepted and hence a vulnerability. Cookie can be read and write  by respective domain only and make sure that right session information is being passed by the client.

Q4.  Why is Reflection slower ?

Ans. Because it has to inspect the metadata in the bytecode instead of just using precompiled addresses and constants.

Q5.  Difference between TCP and UDP ?


Q6.  What things you would care about to improve the performance of Application if its identified that its DB communication that needs to be improved ?

Ans. 1. Query Optimization ( Query Rewriting , Prepared Statements )
2. Restructuring Indexes.
3. DB Caching Tuning ( if using ORM )
4. Identifying the problems ( if any ) with the ORM Strategy ( If using ORM )
Q7.  If you are given a choice to implement the code to either Insert a Record or Update if already exist, Which approach will you follow ?

1. Insert into the DB Table. If exception occurs, update the existing record.
2. Check if the record exists and update it if it exists, If not insert a new record.

Ans. In first case, there would be 2 DB calls in worst case and 1 in best case. In 2nd approach there will be always 2 DB calls.

Decision on the approach should depend on the following considerations -

1. How costly is the call to DB ? Are we using indices , hibernate etc

If calls to DB are costly , 1st approach should be the choice.

2. Exception Book keeping load upon exception.

The benefit of saving 1st call in approach 1 should be bigger than the Book keeping for the exception.

3. Probability of the exception in first apparoach.  

If the DB Table is almost empty, it makes sense to follow Approach 1 as majority of the 1st calls will pass through without exception.

Q8.  What are the considerations to be made in case of loops in Java ?

Ans. 1. It shouldn't result in infinite loop. Please make sure that you have a condition that will terminate the loop and that condition should be reached.

2. Make sure to use the break statement if you aspire to only look for something. Not using break will unnecessarily execute it till the end of for loop in some cases.

3. Similarly use continue to execute the loop with next iteration and bypass the rest of the code block if required.

4. Try to avoid multiple nesting of for loops. If it''s required, Make sure to use break and continue properly so as to avoid some unnecessary processing. 

5. Make sure to use try catch within the loop and not outside the for loop if you expect it to continue if one of the iteration fails.

Q9.  Should we create system software ( e.g Operating system ) in Java ?

Ans. No, Java runs on a virtual machine called JVM and hence doesn't embed well with the underlying hardware. Though we can create a platform independent system software but that would be really slow and that's what we would never need. 

Q10.  What is the difference between JPA and Hibernate ?

Ans. JPA or Java Persistence API is a standard specification for ORM implementations whereas Hibernate is the actual ORM implementation or framework. 

Q11.  What are the advantages and Disadvantages of Sockets ?

Ans. Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications. It cause low network traffic.

Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.

Q12.  Why Struts 1 Classes are not Thread Safe whereas Struts 2 classes are thread safe ?

Ans. Struts 1 actions are singleton. So all threads operates on the single action object and hence makes it thread unsafe.

Struts 2 actions are not singleton and a new action object copy is created each time a new action request is made and hence its thread safe.

Q13.  What is the difference between AWT and Swing?

Ans. Swing provides both additional components like JTable, JTree etc and added functionality to AWT-replacement components.

Swing components can change their appearance based on the current “look and feel” library that’s being used.

Swing components follow the MVC paradigm, and thus can provide a much more flexible UI.

Swing provides extras for components, such as icons on many components, decorative borders for components, tool tips for components etc.

Swing components are lightweight than AWT.

Swing provides built-in double buffering ,which means an off-screen buffer is used during drawing and then the resulting bits are copied onto the screen. 

Swing provides paint debugging support for when you build your own component.

Q14.  Difference between SAX and DOM Parser ?

Ans. A DOM (Document Object Model) parser creates a tree structure in memory from an input document whereas A SAX (Simple API for XML) parser does not create any internal structure.

A SAX parser serves the client application always only with pieces of the document at any given time whereas A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.

A SAX parser, however, is much more space efficient in case of a big input document whereas DOM parser is rich in functionality.

Use a DOM Parser if you need to refer to different document areas before giving back the information. Use SAX is you just need unrelated nuclear information from different areas.

Xerces, Crimson are SAX Parsers whereas XercesDOM, SunDOM, OracleDOM are DOM parsers.

Q15.  Difference between socket and servlet ?

Ans. servlet is a small, server-resident program that typically runs automatically in response to user input. 
A network socket is an endpoint of an inter-process communication flow across a computer network. 

We can think of it as a difference between door and gate. They are similar as they both are entry points but they are different as they are put up at different areas.

Sockets are for low-level network communication whereas Servlets are for implementing websites and web services