Top 10 Java interview questions

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.

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

Advertisements

7 thoughts on “Top 10 Java interview questions

    1. edgblog Post author

      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.

      Reply
  1. Kevin

    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 hate this one. 😉 I know equals uses hashcode.. something like
    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.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s