|
|
06.29.2004 |
|
||||||||||
| Interview Questions | ||||||||||||
I've been a computer geek since a boy, and thoughts related to computers and software engineering get dropped here for the benefit of humanity and my own hubris.
|
Many years old, while sitting in a job interview, the interviewer asked me to describe the garbage collection algorithm for Java. I just happened to have attended a seminar on the changes in Java 1.2 the week before, and there was a lot of discussion on the changes made to the garbage collector. Needless to say, I really impressed him, and he offered me a job. We’ve all been there, where some person is trying to gage your technical ability and ask you questions that will measure yourself. These questions are either general questions (which you should know if you put the acronym on your resume) or the questions are usually so obscure that most of us just roll our eyes and say, “When I need that, I’ll just look it up.” However, it seems that the interviewers are asking better questions this year… that and I think they have a private forum somewhere, since I am asked the same questions in every interview. So I thought I would publish the answers here. Note: In case I am the one doing the interviewing, and you are smart enough to find this page before the interview, then you are probably smart enough for me to hire you. In Java, what is the difference between a abstract class and an interface? While you can’t instantiate either one, this is where the similarity ends, and it is probably this reason why people ask it. An abstract class is marked abstract because you want a class that contains methods that can only be used by children classes that extend it (and supposedly add some remaining ingredients to make the class valid). This allows a class to contain code, but can’t be instantiated (for whatever reason you need). An interface, on the other hand, is really an API contract. It contains no code, just the list of methods that any class claiming to implement it needs to create. These two things have such a different semantic meaning that I was surprised the first time I heard it. But it has been asked in every one of my screening interviews. As a design note, if you are building a library, make your users implement an interface instead of extending one of your classes, since Java doesn’t do multiple inheritance, it is mean to use up a user’s one chance at extension. In Java, what is the difference between Collection classes (added in version 1.2) and the Hashtable/Vector classes (in version 1.0)? While the Collection classes create a better model for dealing with data structures, the biggest difference is that the Hashtable and Vector classes are synchronized and the Collection classes are not. What this means is that every interaction with a Vector has a slight performance hit as it needs to acquire a lock on the underlying data. Yes, it is slight, but with 99% of the uses of data structures, it is unnecessary. What this means if you share data structures between multiple running threads, you either use the Vector/Hashtable or you do your own locking with the Collection classes. In SQL, what is the difference between an inner and outer join? I’m not an SQL-wiz, but have used both types of joins, but didn’t know them by these names (ok, I’ll admit that I’m an SQL hack). Still, it is good to know this. An inner join is your typical join where your result set lists only the matching rows in both tables. An outer join will list all of the rows from one table (whether they match or not), so in this join, the order of the tables is important. This is often called a “left” join… SELECT * FROM clients LEFT JOIN orders WHERE clients.id = orders.clientid In this example, all of the clients are listed, whether or not they have an order. If you replaced “LEFT JOIN” with a simple comma (for a typical inner join), then only customers that have an order will be listed. In Java, what is the difference between the IO streams and the Reader/Writer classes? The biggest difference (aside from some minor interface changes and being slightly more efficient) is that the Reader/Writer classes (or Character Streams) can deal with differences in character encodings (multibyte characters like Unicode) and do not assume that a single byte is equal to a character (as some of the Stream classes do). See Sun’s documentation on I/O. The original classes that supposedly dealt with characters (but really didn’t) have been deprecated. But if you need to deal with byte-oriented data, then you can still use them. One of my irritations with Java is the fact that while you can upgrade the API and deprecate older/badder classes, the entire library needs to be upgraded as well. It is painful to have a “Reader” but can’t pass it over to another class library that only accepts “Stream” classes. (Streams can be converted up to a Reader/Writer, but the reverse is not possible). General Knowledge Questions Looking for more questions? Well, there are a number of “general purpose” questions about Java that everyone should be able to ask. For instance:
Conclusion? I can’t believe that in one interview I was asked to pseudo-code a doubly-linked list data structure. Are you kidding me? I suppose we could still ask juniors this, but while I can still do these in my sleep, it is almost always better to use a standard library that has been debugged and optimized than “roll yer own.” Oh, and I hope nobody asks me an combinatorics questions, for I’ve certainly flushed those brain cells away years ago. Thought originally posted on Tuesday, 29 June 2004
© 2004-2006, Howard Abrams • Except where otherwise noted, all original content is licensed under a Creative Commons License (see details). A comment to this from the Author
I’m sorry, but I did forget to answer the question about the garbage collection. I’m too tired to actually write out the answer, so I’ll leave it as an exercise for the reader. That’s mean. You’ll probably find what you are looking for here. Improvements made in the GC in Version 1.4 are described here (and you can warp your brain here). Also, check out these lecture slides for an overview. By the way… a JVM doesn’t have to have a garbage collector, but most of them do. Comment posted on Wednesday, 30 June 2004 |
|||||||||||
|
||||||||||||