Wednesday, June 3, 2009

Java interview with an Antivirus company.

I had written this an year ago but not published yet so I decided to publish it today.

A while back I interviewed with a company for a Java programmer contract. Here is how it went and questions he asked:


1) Servlet life cycle: I said init(), execute() and destroy().
I had totally forgotten that the execute() method does not exist in a http based servlet. So it is rather:

init() - where we initialize connections etc.
service() - from where it calls either doGet() or doPost() methods.
destroy() method - where we free any resources used etc.

2) How do multiple application servers obtain a lock on file (say for writing) - that is synchronization between two application servers - I wasn;t sure of this! I told him that we could get access to the System class and then get access to the particular file handler and perhaps call a sysnchronize or lock method on the file handler! He wasn't really impressed with this. Another way I came accross was to rename the file just before the code starts to write to the file and handle FileNotFoundException - basically hide the file from others while writing to that file.

3) How do you compare two files? I told him either using comparable interface to comparator interface. When using comparable interface we have to write comprareTo() and in comparator we have to write compare() method.

4) Threads in servlets - can you use them? I told him yes we could use them, but it is not a good idea to have threads in servlets. My reasonsing is that Once the servlet spawns the threads and dies, what would happen to the spawened thread? There is no definite way to track the threads that were spawned.

Also I came accross ServletContextListener() interface which gives a handle to the ServletContainer (one for the whole application) using which we could start and handle threads, initialize connection to databases, handle init parameters etc. The two methods in ServletContextListener - contextInitialized and contextDestroyed are called upon initialization of the application and upon its closing respectively. There is only one servletContext for all the servlets.

No comments:

Post a Comment