java interview questions

Top java frequently asked interview questions

Using to fire and handle HTTP requests

Use of is asked about pretty often here, and the Oracle tutorial is too concise about it.

That tutorial basically only shows how to fire a GET request and read the response. It doesn't explain anywhere how to use it to among others perform a POST request, set request headers, read response headers, deal with cookies, submit a HTML form, upload a file, etc.

So, how can I use to fire and handle "advanced" HTTP requests?

Source: (StackOverflow)

Java += operator

Until today I thought that for example:

i += j;

is just a shortcut for:

i = i + j;

But what if we try this:

int i = 5;
long j = 8;

Then i = i + j; will not compile but i += j; will compile fine.

Does it mean that in fact i += j; is a shortcut for something like this i = (type of i) (i + j)?

Source: (StackOverflow)

When to use LinkedList over ArrayList?

I've always been one to simply use:

List<String> names = new ArrayList<String>();

I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code.

When should LinkedList be used over ArrayList and vice-versa?

Source: (StackOverflow)

What is a serialVersionUID and why should I use it?

Eclipse issues warnings when a serialVersionUID is missing.

The serializable class Foo does not declare a static final serialVersionUID field of type long

What is serialVersionUID and why is it important? Please show an example where missing serialVersionUID will cause a problem.

Source: (StackOverflow)

Creating a memory leak with Java [closed]

I just had an interview, and I was asked to create a memory leak with Java. Needless to say I felt pretty dumb having no clue on how to even start creating one.

What would an example be?

Source: (StackOverflow)

Difference between public, default, protected, and private?

Are there clear rules on when to use each of these when making classes and interfaces and dealing with inheritance?

Source: (StackOverflow)

Is Java "pass-by-reference" or "pass-by-value"?

I always thought Java was pass-by-reference; however I've seen a couple of blog posts (for example, this blog) that claim it's not. I don't think I understand the distinction they're making.

What is the explanation?

Source: (StackOverflow)

Iterate through a HashMap [duplicate]

Possible Duplicate:
How do I iterate over each Entry in a Collection Map?

What's the best way to iterate over the items in a HashMap?

Source: (StackOverflow)

"implements Runnable" vs. "extends Thread"

From what time I've spent with threads in Java, I've found these two ways to write threads:

With implements Runnable:

public class MyRunnable implements Runnable {
    public void run() {
//Started with a "new Thread(new MyRunnable()).start()" call

Or, with extends Thread:

public class MyThread extends Thread {
    public MyThread() {
    public void run() {
//Started with a "new MyThread().start()" call

Is there any significant difference in these two blocks of code ?

Source: (StackOverflow)

Create ArrayList from array

I have an array that is initialized like:

Element[] array = {new Element(1), new Element(2), new Element(3)};

I would like to convert this array into an object of the ArrayList class.

ArrayList<Element> arraylist = ???;

Source: (StackOverflow)

Why does this code using random strings print "hello world"?

The following print statement would print "hello world". Could anyone explain this?

System.out.println(randomString(-229985452) + " " + randomString(-147909649));

And randomString() looks like this:

public static String randomString(int i)
    Random ran = new Random(i);
    StringBuilder sb = new StringBuilder();
    while (true)
        int k = ran.nextInt(27);
        if (k == 0)

        sb.append((char)('`' + k));

    return sb.toString();

Source: (StackOverflow)

Proper use cases for Android UserManager.isUserAGoat()?

I was looking at the new APIs introduced in Android 4.2. While looking at the UserManager class I came across the following method:

public boolean isUserAGoat()

Used to determine whether the user making this call is subject to teleportations.

Returns whether the user making this call is a goat.

How and when should this be used?

Source: (StackOverflow)

How can I create an executable JAR with dependencies using Maven?

I want to package my project in a single executable JAR for distribution.

How can I make Maven package all dependency JARs into my JAR?

Source: (StackOverflow)

How to efficiently iterate over each Entry in a Map?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?

Will the ordering of elements depend on the specific map implementation that I have for the interface?

Source: (StackOverflow)

Is there a unique Android device ID?

Do Android devices have a unique ID, and if so, what is a simple way to access it using Java?

Source: (StackOverflow)