java interview questions
Top java frequently asked interview questions
I want to create a list of options for testing purposes. At first, I did this:
ArrayList<String> places = new ArrayList<String>();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");
Then I refactored the code as follows:
ArrayList<String> places = new ArrayList<String>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
Is there a better way to do this?
Source: (StackOverflow)
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)
Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?
Source: (StackOverflow)
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)
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)
Use of java.net.URLConnection
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 java.net.URLConnection
to fire and handle "advanced" HTTP requests?
Source: (StackOverflow)
How do I use JUnit to test a class that has internal private methods, fields or nested classes? It seems bad to change the access modifier for a method just to be able to run a test.
Source: (StackOverflow)
How can I convert a String
to an int
in Java?
My String contains only numbers and I want to return the number it represents.
For example, given the string "1234"
the result should be the number 1234
.
Source: (StackOverflow)
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)
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() {
//Code
}
}
//Started with a "new Thread(new MyRunnable()).start()" call
Or, with extends Thread
:
public class MyThread extends Thread {
public MyThread() {
super("MyThread");
}
public void run() {
//Code
}
}
//Started with a "new MyThread().start()" call
Is there any significant difference in these two blocks of code ?
Source: (StackOverflow)
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)
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)
break;
sb.append((char)('`' + k));
}
return sb.toString();
}
Source: (StackOverflow)
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)
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)