Monday, May 28, 2007

Tell Me Why (Java/J2ee Interview Questions)

Interview : The word itself would draw out different kind of emotions. Some may get excited. Some may get challenged. For first timers it may bring pressure in the form of expectation. And of course their are some who would get scared.
The very fact that I am writing about this should be an indication in itself that my feelings are different from the usual written above. There are few people who are actually left confused by the interviews and I am one of them. After every interview I have given (of course in the software industry) I am always left wondering why the questions which were asked were ever needed to be asked at all. It is not that I knew the answer to all of the questions asked rather they were asked left me baffled. I have never been able to understand and appreciate the psyche of an interviewer. It is because of the respect which I have never been able to give to this form of evaluation, I have managed to flunk every time. I have only managed to scrape through one interview and that was for the company in which I am in right now (My first Job). That was more so because the interviewer (who became my PL later on) was asking questions more so about my project rather than the clichéd general interview stuff. These interviews have always left me with the funny feeling. I will try and elaborate using an example.
Let us say you have been inside the industry for a year and then you start working on a job switch. The arrogance of the knowledge you displayed during the first time interview you gave comes back and you start thinking big of yourself. You never realize that a year in the industry hasn’t given you much experience in the eyes of the interviewer and you would rather be asked the same level of question as you were asked before. The only difference being the language on which you will get evaluated will change from c/c++ to java/j2ee. Hasn’t it happened with you if not then probably I am an exception. Even if I am it doesn’t matter, I would rather prefer thinking otherwise. Giving interview again after a year (rather a few months before) gave me the feeling of déjà vu’. The same frustration which I used to feel starting to come back but their was a difference this time. This time I was much more relaxed and was able to laugh at those frustration. I will try to explain the precarious scenario in which I was with a typical java/j2ee interview question and answers and the feeling which comes along with it.
A typical java/j2ee have a set of clichéd questions. And no matter how much one studies a normal human being will never be able to cover all of them. Java/J2ee is like an ocean and the candidate can easily be floored on the mat at any given point of time. It all depends on the person who is taking the interview. If the packages he like belongs to the set of one you like then probably you are safe. Otherwise you very well know that interviewers love to grind candidates and push them to a corner. I think every interviewer gets a kick out of demeaning the knowledge of the candidate. The knowledge neither the candidate nor the interviewer would have never used nor will ever used in the real world..

1) I have never been able to understand why does anyone ask about different packages and their implementation. How the hell is anyone suppose to know about the classes inside and their implementation. There are thousands of them and which one is your favourite Mr. Interviewer I never knew otherwise I would have studied and not make a fool of myself.

2) Now every class by default extends the object class. While writing the code did you ever care about it and moreover did one ever cared about the methods of the object class. Some of the favourite methods to be asked about are clone(), equals(Object obj), finalize(), hashcode() and a few other thread specific methods. Hashcode() Returns a hash code value for the object by converting the internal address of the object onto an integer. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. Some of the properties of this method is that if two object is equal it returns the same hashcode. If not it returns different hashcode (Although not necessary it done to improve the performance of hashtable). The determination of object equality is done during the equal() method of the object class. Tell me one thing who uses object hashcode to create a table. Anyone creating an object will definitely have unique attributes which would form the key for storing them. finalize() another one of the crazy method called by garbage collector but did anyone ever override this method in their own class. If yes then you do tell me why. The purpose of other methods will get clearer later on in the rendition of my grievances.

3) Difference between == and equals another favourite of the interviewers. When two objects are compared using == it returns false whereas what happens if it is done via equals (obj1.equals(obj2)). Well if one hasn’t been using equals that extensively one might go back to strings and say Hey strings are objects , it works over there then it should work for any object. But hold on my friends this where the equals method of the object class needs to be overridden as an object can be made according to programmers whims and wishes so the equals need to be overridden for the method to work on our own customized classes. Well most of the time who cares about the equals method when the comparison needs to be done on a particular attribute only.

4) Collections: Probably one of the most favourite among the interviewer. The questions rather interrogation starts of lightly with interviewer expecting you tell him/her with the classes you are familiar with. Then question will go in the direction of the difference between a linked list and array list and when to use which. You say for that for faster access you will use the later but they have an answer already in their head which they want to hear. Yes they actually want you to say that for linked list you have exact memory allocation whereas an array list is growable array in which the array size increases dynamically by a factor of 1.5 + 1 with an initial size of 10. Tell me one programmer who took this into consideration. Its generally array list everywhere so you also start using the array list or any other storage structure which one is familiar with without thinking about the concept being discussed here . Then their may be other question on Set which cannot have duplicates SortedSet which is the sorted implementation of Set. Then they may ask some question on some legacy classes like vector which are synchronized.

5) Then there are good old legacy object oriented question but specific to java. Some of them are A) Difference between interface (which can have only abstract methods and static final variables) and abstract classes which may have self implementation of a method besides the usual abstract methods. B) Whether a protected method in base class can be declared as public derived class. The answer for which is no as we can only reduce the scope not increase it. Interviewers generally have the habit of tweaking a little here and a little there of the OOP concept and they feel glad to grill the candidates with them.

6) Of course then their are some basic key word question which interviewers definitely like to grill you with. Falter here then it would be hard to impress them. The key words can be static, final, transient, public, private, protected, default etc. 'static' is for declaring class variables i.e. only one reference is created for static variables for multiple objects. final variables are constants, final methods cannot be overridden and final class does not make any sense for extension. transient variables are not serialized (i.e. when a object copy is created it gets initialized by its default value). private is for class specific variables and methods, protected variables and methods can be used by derived classes, default variables and methods are accessible within a package only and public variable and methods is accessible across packages. Now just one question is on my mind does anyone uses 'default' and unless one come across a scenario while coding that their is a requiremnet of such declaration for the design why would anyone even bother to think about it.

7) Coming to exceptions. Who the hell likes exception in the program. Every programmer would like to exhaustively handle all kind of exception scenarios. So much effort gets spend in identifying and handling those scenarios but any programmer would be able to leave a few. Thankfully we have certain classes to catch them. The Checked (which can be catched,subclass of Exception class)and Unchecked(which only gets caught at runtime, subclass of Runtimeexception class) exceptions are the two kind of exception in java. Now the most generic question to be asked under this category is the usage of finally block along with try and catch. Its basically for calling this block(generally to free resources) irrespective of try block is called or catch block. But this is not enough interviewers have a cruel way of confusing you by asking whether this block gets called if in a try block their is a return statement. The answer is yes but they ask in a way that you will definitely get confused and will struggle to get a reason why and then you try your luck. Their is another thing they ask that is the way to create ones own checked and unchecked exception. One can throw checked exception using throw new Exception() and unchecked exception using throw new RuntimeException(). Yes simple but to that person who generally uses unchecked exception but my guess and experience tell me if I want to throw my own exception I would throw a checked exception why should I require to throw an unchecked exception may be someone can explain. Something which I wont be able to catch within the program is a scenario I haven't come across.

8) Object clone(): Here is another one of the crazy concept if one doesn't not know one is bound to flounder. If one doen't know this concept one generally takes String as a reference and derive conclusion out of it. Here lies the inherant danger of the concepts getting totally screwed up because of the implementations provided in string. When obj1=obj2 is done obj2 reference gets passed to obj1. To create a new copy clone() method of the object class has to be used. Then again clone() can only give shallow copy that is a copy of the object but the references inside the object is not created. To get a complete copy of an object i.e. to do deep copy one has to make the object serializable and write it to (Byte)OutputStream and read it back from there. This is probably the only way to create a total copy of an object including its references. My Oh My ....Somebody save the candidates if they did not know about cloning.

My friends I have a long list following. I was able to write this much only. I will come back and update with a few more as I start recalling the typical clichéd questions.

2 comments:

anonymous coward said...

i guess that by asking such stupid questions, the interviewer is trying to hide his own ignorance. how the hell can anyone be using all the classes and methods of java collections ? you dont write code in isolation. it has to fit in with existing code. and if your existing code spans the length and breadth of java, i dont want to be the person who has to maintain it :P!

Rohan Rai said...

Yes man thats what I was trying to express via this post. Thanfully somebody understands