The 10 most frequently-asked Java interview questions, in my experience.
Mostly used in phone interviews to “weed out” the weakest candidates.
These questions are fairly simple so getting them wrong will raise a major red flag.
For the same reason I wont bother printing the answers, unless somebody specifically asks for it in the comments.
In no particular order:
- How to prevent concurrent access to a method
- Meaning of the volatile keyword
- Difference String and StringBuffer
- Difference between ArrayList and Vector
- Relationship between equals and hashcode
- Difference between checked and unchecked exceptions
- Meaning of the final keyword
- Explain garbage collection, can it be forced
- Difference between an interface and a abstract class
- what’s a deadlock
Maybe you could add some hints about the difference between the answers for question 4 and 8?
Comment by Daniel Lindner — March 30, 2010 @ 9:49 pm |
That’s only nine.
Comment by Curt Cox — March 31, 2010 @ 1:47 am |
Hmm… that’s 9.
Comment by Dimitris Andreou — March 31, 2010 @ 4:04 am |
Dimitris, Curt and Daniel: thanks for letting me know about the typo, now corrected.
Comment by edgblog — March 31, 2010 @ 7:04 am |
How about “do you use design patterns”?
Comment by ct — March 31, 2010 @ 12:15 pm |
That’s another good one. Good in the sense that it’s frequently asked only, as it’s otherwise too vague a question to demonstrate real technical ability.
Comment by edgblog — March 31, 2010 @ 8:52 pm |
I’ll try to answer in short.. someone tell me if I am wrong/way off:
- How to prevent concurrent access to a method
Use synchronized keyword in method declaration. Can also make class synchronized.
Can also use synchronized { } block inside method to limit scope of synchronized code.
- Meaning of the volatile keyword
attempt to use hardware such as 3D hardware or cpu registers
for variable defined with volatile.
- Difference String and StringBuffer
String is immutable, each operation on STring creates new String. StringBuffer
is mutable object, methods like append() don’t create new string but work
on the underlying char buffer.
- Difference between ArrayList and Vector
ArrayList is not synchronized, Vector is. In today’s JDKs (1.6+) there is
almost no performance difference however.
- Relationship between equals and hashcode
I know equals uses hashcode.. something like
I hate this one.
two objects equals and hashcode must be same for object to be same..
- Difference between checked and unchecked exceptions
this one I forget.
- Meaning of the final keyword
can’t subclass a final class, or for variables makes them
immutable once assigned, often used with static keyword to make
constants.
- Explain garbage collection, can it be forced
GC is the process of keeping counter of references to objects and freeing the
memory they occupy if no longer referenced. You can’t force a GC, but you can
“hint” that you need it done sooner, sometimes possibly speeding up when a GC
occurs. I don’t know if the double gc() call works, but some have hinted that
it helps.
- Difference between an interface and a abstract class
interface is definition of methods that a class must implement. All
methods must be implemented. Abstract class implements some methods
(or not) and leaves some unimplemented (abstract). Subclasses must
implement the methods that are abstract. Interface is essentially a
completely abstract class.. only you “implement” an interface, not
sub-class it (although you can sub-class interface with another interface)
- what’s a deadlock
deadlock is when two threads are waiting on the other to finish, catch 22 style.
I honestly answered these without looking anything up. Hence why one I can’t remember and the other (equals/hashcode) I forget exactly.
The issue of design patterns to me is a waste of a question. I’ve been in many companies and while we do implement various design patterns, I’ve never specifically been told to do so, or never has it been brought up in discussion on what design pattern to use for something. Much like using UML, it’s almost never done in most companies.. but it is essential to have an understanding of at least a few common design patterns.
Comment by Kevin — April 3, 2010 @ 4:42 am |