I had a technical interview last week for a Java Software Engineer position. They asked some pretty basic questions such as:

1) What's the difference between an abstract class and an interface? Which do you use more?
2) How does garbage collection work in Java?
3) What is the difference between pass-by-value and pass-by-reference?
4) What is inheritance?
5) What is encapsulation?

Then the interviewer asked these questions which I wasn't too sure about:

6) What good coding practices do you follow?
7) What is the difference between an ArrayList, HashSet, and KeySet?
8) Which is better for garbage collection: allocation of 128 MB or 1 GB of memory?
9) What are some performance issues you've encountered with Java?

I feel like I did rather well on questions 1-5, but I feel like I struggled a little on questions 6-9. I answered 6-9 the following way:

6) Proper error/exception handling, limiting scope of variables, restricting access to variables and methods (encapsulation), promoting maintainability and readability (well commented code), using interfaces more than abstract classes when applicable (since implementing interfaces provides more flexibility).

7) ArrayList - Resizable data structure which allows you to store any data type of type Object. ArrayLists are not synchronized.
HashSet - Allows null elements. Constant time for add and remove operations.
KeySet - Is a set containing all the keys from a HashMap. (This confused me because I didn't consider a KeySet a data structure)

8) I wasn't too sure how to answer this question. When the interviewer asked me this question, I said 1 GB was better since garbage collection wouldn't occur as much. He disagreed with me and said that 128 MB was better because garbage collection wouldn't take as long. I responded by saying that it depends on the demands of the Java application and that allocating less space would more likely result in OutOfMemoryErrors. I then said there's a trade-off. He simply said, "Oh, okay"...Eek!

9) This question stumped me. I simply said that Java might be considered slower than some languages. I gave an example of how one of the projects at my current company tried converting C++ code to Java and how the performance was a lot slower compared to C++. Based on his reaction to my answer, he didn't seem too impressed. After my interview, I researched online about performance issues with Java, but didn't find anything useful.

So my question is, how did I do on questions 6-9? What did I miss in each of those questions? How would you have answered these questions?

I would really like to nail my next interview if these questions are asked again (whenever that is)!

Thanks in advance!

Edited 2 Years Ago by Compton11: Update topic

I gave an example of how one of the projects at my current company tried converting C++ code to Java and how the performance was a lot slower compared to C++. Based on his reaction to my answer, he didn't seem too impressed. After my interview, I researched online about performance issues with Java, but didn't find anything useful.

Saying that in a Java interview... LOL you got some balls.That's like heresy. I mean we all know Java is slower compared to c++ but it's not what the interviewer wants to hear. Unless that program you were converting was doing stuff like game programming or intense CPU calculations (hundreds of thousands) then speed comparsions have proven to be negligable due to the massive improvements with JIT.

It might have just highlighted that you have inadequate programming skills...

Even GUI programs which use swing2d with native calls to the OS have proven to be equal/non significant now in speed.

Edited 2 Years Ago by iamthwee

Overall, I think you did pretty well on the interview questions, though you are correct that #9 could have been handled better, such as mentioning that GC in Java has a serious impact upon long-running applications without considerable effort to compartmentalize the GC domains or to provide object caches for frequently used/discarded types such as strings. We do a LOT of java programming in my company, both on mobile devices as well in our servers, and GC has ALWAYS been the biggest performance leech. Mostly that is due to the standard Java/Dalvik GC using mark-and-sweep algorithms which are by nature non-deterministic and unsuitable to hard real-time systems.

Edited 2 Years Ago by rubberman

1) "which do you use more". Since when are we in a popularity contest between interfaces and abstract base classes?
2) implementation dependent. Wouldn't expected to need to know that in detail unless I'm to write a JVM.
8) "better" is relative to what you want to achieve...
9) last several years, none related to the language or platform, applications are generally faster than the networks and databases they rely on.

Not an interview that'd give me much confidence in the expertise of the people putting the questions together.

I always find it funny how interviews, which presumably are aimed at getting to know that you are not just a pedestrian CS student and that you have some real experience and insights, end up asking a bunch of pedestrian CS questions.

1) What's the difference between an abstract class and an interface? Which do you use more?

I don't care much about this, I'm primarily a C++ guy, and we don't make such a forced distinction (which exists only because of a language limitation in Java). I generally favor simple inheritance hierarchies and more reliance on composition than inheritance, and for those reasons, I use abstract base classes more than interfaces because they make more sense in that kind of structure. The use of interfaces encourages the creation of monolithic classes with too many disparate functionality and purposes. I prefer the composition of nuclear objects of classes with simple-and-flat inheritance and a single, well-defined purpose.

2) How does garbage collection work in Java?

That is none of my concern as a programmer (as jwenting says). All I need to know is that I create stuff, share it at will, eventually discard it, and that at some indeterminate time in the future that discarded memory will be freed (and recycled). If I have to guess, the JVM probably implements this with some sort of reference counting or some other semaphore-like mechanism, and regularly cleans up any memory that is no longer referenced anywhere (and thus, no longer needed).

3) What is the difference between pass-by-value and pass-by-reference?

Pass-by-value means it makes a copy of the parameter as a local variable to the function. Pass-by-reference means it passes a handle / pointer / reference to the original object that was passed, meaning that copy is avoided and changes on the object within the function also change the original object.

4) What is inheritance?
5) What is encapsulation?

Well... talk about pedestrian questions. I don't feel like answering such boring questions.

6) What good coding practices do you follow?

Proper error/exception handling, limiting scope of variables, restricting access to variables and methods (encapsulation), promoting maintainability and readability (well commented code), using interfaces more than abstract classes when applicable (since implementing interfaces provides more flexibility).

I think that your answer was pretty good. You listed many of the important things. However, I think it lacks a bit of a broader perspective. It sounds a bit too much like "I know how to write impeccable code for a school assignment". What about broader things like version control, unit testing, UML diagrams and other aspects of software design methodology. What about softer skills like communication and team-work. If you mention those as "good coding practices" you might strike on a good cord with the interviewers, because it shows you understand coding in its wider context.

7) What is the difference between an ArrayList, HashSet, and KeySet?

ArrayList - Resizable data structure which allows you to store any data type of type Object. ArrayLists are not synchronized.
HashSet - Allows null elements. Constant time for add and remove operations.
KeySet - Is a set containing all the keys from a HashMap. (This confused me because I didn't consider a KeySet a data structure)

My guess is that by "KeySet", they meant "Set", as in, the container that stores the elements in a binary search tree, and achieves log(N) lookup and insertion times. I think that the aim of this question was to compare and contrast those three fundamental data structures: contiguous array, hash table, and binary search tree. I think, that's what they were looking for, to see how good your fundamentals are in terms of data structures.

8) Which is better for garbage collection: allocation of 128 MB or 1 GB of memory?

I think that question is just ill-defined. What does "better" mean? Of course, allocating 128Mb should be quicker than 1Gb, but if you need 1Gb, then you need it, and allocating 128Mb is insufficient, and thus, very bad. This whole question makes no sense. If you compare allocating 10 times 100Mb instead of 1Gb once, then you can have a conversation.

I think that the interviewer probably had something in his mind that he hoped people would answer to that question, and manufactured to question to try and trigger that response. But he failed to do so, because the question makes no sense.

9) What are some performance issues you've encountered with Java?

That's a weird question too. Obviously, Java is not geared towards performance, and it seems kind of pointless to dedicate too much effort on this issue. Java works well when performance doesn't matter (i.e., not CPU-bound), and when it does, you wouldn't use Java. So, what's the point?

Anyways, I think that you could easily just mention some of the general performance issues that are commonly encountered in Java, off the top of my head: overhead incurred by the garbage collector (as rubberman pointed out), memory fragmentation due to frequent small dynamic allocations, thrashing of the cache architecture with abusive indirections, latency in accesses of "synchronized" data structures, etc... I mean, take your pick, Java is rife with performance issues.

Based on his reaction to my answer, he didn't seem too impressed.

That's probably because who cares how Java compares to C++... that's not what he asked. Not all Java code is the same, just like not all C++ code is the same. Performance of an application is primarily a function of the skills of the programmer, the time invested in optimizing it (empirically), and some of the core overhead or limitations of a language. He wanted to know how well you would do on the first point, i.e., your knowledge about performance issues and your skills at solving them. He wasn't interested on your take on how the core overhead and limitations of Java stack up against an infrastructure, zero-overhead, high-performance programming language like C++. It's important, in an interview, to understand what the interviewer is looking to discover about you, otherwise, you end up answering the wrong questions, and then, it doesn't matter if your answers are "correct" or not.

allocating less memory may well make the application slower as the garbage collector kicks in much more frequently...
Each run may go quicker, but the higher number of total runs can be detrimental.

This article has been dead for over six months. Start a new discussion instead.