views:

5032

answers:

34

I was going through Questions every good .Net developer should be able to answer and was highly impressed with the content and approach of this question, and so in the same spirit, I am asking this question for Java/J2EE Developer.

What questions do you think should a good Java/J2EE programmer be able to answer?

I am marking this question as community wiki as it is not user specific and it aims to serve programming community at large.

Looking forward for some amazing responses.

EDIT: Please answer questions too, as suggested in the comments, so that people could learn something new regarding the language, too.

+37  A: 

What is the relationship between hashCode() and equals()? What is the significance of these methods? What are the requirements for implementing them?

Jeff
Yes, every dev should at least be aware of the problems, even if you will not necessarily encounter them in practice.
sleske
equals() and hashCode() in java @ http://www.technofundo.com/tech/java/equalhash.html
pramodc84
+30  A: 

What is the difference between Set, Map and List?

I'm still amazed how many people don't know this one in a telephone interview.

Jon
Granted, but it's a matter of 20 secs looking at the docs on the net ("java collections framework"), so many worthwhile people may not bother with keeping it in memory.
Manur
Nope sorry don't buy that, it's basic knowledge that every developer should know.
Jon
I'd go as far to say anybody even thinking about a job in software needs to understand not only that, but also differences between different kinds of lists (ArrayList/LinkedList). Not necessarily best implementation or even O(n) notation, but an understanding of when it's right to use what.
Milan Ramaiya
+1 @Jon: I usually subscribe to the "no need to memorize details" school of thought, but if you don't even know the basic conceptual differences between those basic interfaces, then you're definitely not a good Java developer.
Joachim Sauer
@Joachim: you could drop the word "Java" from your statement. Sets, Maps, and Lists are basic algorithmic notions.
CPerkins
So many devs just use lists. I've had the misfortune of meeting someone who wrote methods to implement set semantics around a list. +1 as a simple but useful question to find out if someone has half a clue.
Spence
How does that qualify a Java EE developer?
BalusC
I agree. If you don't know this simple thing I would not hire you.
Shervin
@BalusC What do JavaEE developers not use collections or something? :)
Jon
A: 

Describe the differences between the "four" (not three ;)) types of inner class..

mP
Do you mean the four types of _nested_ class (http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html)? There are static nested classes, inner classes, anonymous classes and local classes, with only the last three being inner classes with a parent instance.
Andrew Duffy
great idea, ask them about things they should not use.
01
@01 - Why should one not use inner classes? They are ubiquitous as event handlers in Swing programs, and functional collections libraries like Google Collections depend on them.
Andrew Duffy
What i meant by inner are the types one declares inline inside a method. If you really want you can extend to include other local types.
mP
The better question is why would you use Swing
Woot4Moo
+11  A: 

Trick question: What kinds of parameters are passed by reference in Java?

It's amazing how many people still parrot the "primitives are passed by value, objects are passed by reference" mantra.

Chinmay Kanchi
I agree totally with you. I had struggled a lot with this discussion.
Rachel
I wanted to post "Is Java pass by value or pass by reference?" but was afraid it'd cause an internet argument
matt b
I think most people on SO have the right of it. It's not really a never-ending argument, because there is only one right answer, even the Java spec says so, IIRC. :)
Chinmay Kanchi
I wish I could upvote this more than once - I actually encountered this subtlety while developing, understanding what really goes on is a real eye-opener
laura
I wonder if the correct answer is always the one to give in an interview. In a place I interviewed for, the interviewer also parroted the same thing.
MAK
all are by value including the reference.
mP
I've noticed a difference between folks who studied C as a first language and folks who studied C++ as a first language in answering this question about Java. Probably because the C users are taught that passing a pointer to a value is a "pass by reference".
Uri
I actually learnt C first, and it did take me a long time to wrap my head around the fact that passing a pointer is not the same as passing a reference, but that doesn't excuse ignorance, only somewhat mitigates it :).
Chinmay Kanchi
That's a stupid question as the "wrong" answer will most likely become technically right if you also ask *"what consequences has passing by reference?"*. It's just a matter of definition of what *pass by reference* means and is thus of not much value in the Java world as there is only one way to pass objects. It's more important to understand what really happens than to use a aribitrary definition for *pass by reference* the way you want.
x4u
This question is at least problematic, because it mostly hinges on the exact definition of "pass by value" / "pass by reference", which is often used incorrectly. While I agree that a good developer should know the definitions, it's more important to understand the consequences. If you understand Java's actual behaviour, then knowing the correct name for the concept is not very meaningful, because it's not something you discuss in practice, as it never changes.
sleske
Agreed. However, I've seen that a lot of Java developers who dabbled with C++ in college think that "pass by reference" means the same thing in both languages.
Chinmay Kanchi
@Uri Isn't it? :-o
Petr Peller
Erm, objects *are* passed by reference in Java. You cannot pass an object by value; among other things, objects never exist on the stack. *References* are passed by value, sure.
Sean Owen
@MAK, yeah but that is a sign that you don't want to work there...
Zombies
A: 

Advantages and disadvantages of thread-safe classes and explicitly synchronized code and examples of good applications of both. It is often not correct to trust on thread-safe classes as guarantees for data consistency in multi-threaded applications.

Dan
+11  A: 
sateesh
I would really love to know answer for this question. It sounds very interesting.
Rachel
@Rachel: The reason is because the `final` keyword applied to the `Name` object, not the `name` field of class `Name`
ryanprayogo
Well, the parameter `n` is declared as final, which means it can't be reassigned in the `test` method body. It doesn't cause an error because the method `test` doesn't attempt to reassign the parameter, it merely sets a property on the parameter... `n = new Name("test");` would cause a compile error...
rjohnston
Adding to the @rjohnston's comments: the usage of final for method parameter doesn't ensure immutability of a class instance. If there is a need for immutability the class (Name in the present case) should be designed so (for example in present case don't provide the setter)
sateesh
Thank you all for sharing valuable information. I really appreciate your inputs on this.
Rachel
I'm confused. The above DOES give a compile error. Where is the public class that the test method is part of? Also, the final keyword would appear to have no effect - I don't see what this example shows...?
Alex Spurling
Sorry I notice that the ctr of Name class was private,changed it to be public.The example is not complete (they are code snippets).To get rid of compile error:a.Create a public class by name "Name" b.Create another public class (Test) that has method "test" c.In Test have a main method creating Name (Name n = new Name("hello");). Create Test instance in main and call test new Test().test(n). final keyword doesn't ensure that instance being passed isn't modified, it ensures that the ref is pointing to the same instance. Adding line: n = new Name("Kind") in the "test" gives compile error.
sateesh
+7  A: 

Many questions and interviews are available at http://www.techinterviews.com/interview-questions/java and I don't really see value in copy / pasting a selection of them.

No, it's up to you to create your own compilation of things you think are important. Personally, I proceed always in two steps: first a few questions to get a basic idea of the experience and skills, then a problem solving situation. I'm indeed not convinced that being able to answer any known questions makes you a good or bad unknown problems solver. So, I prefer to ask people to solve a given problem, to give them some requirements, and ask them to write code (but not on paper). I give them some time to come back to me and check how they did it, their coding style, how they used the suggested APIs, etc.

That all being said, my favorite question is "what don't you like about Java?" (in the spirit of this one). It is really a excellent question, it gives you an immediate feedback on how much a candidate has used Java and explored its API and if he just religious about it or not (as the OP wrote).

Update: As suggested by CPerkins, a better wording for the question suggested above might be "What would you most like to see changed in Java?". And indeed, I prefer this way.

Pascal Thivent
I dont like question "what don't you like about Java?", i would lie, because it sounds just like "why you left your last company?". plus they might think - "if you hate java we wont offer you the job".
01
@01 It's your right but I don't agree with you and I think that you missed the point of that question.
Pascal Thivent
I've usually seen this put more like: "What would you most like to see changed in Java", rather than "don't like".
CPerkins
@CPerkins That's a better wording indeed, better than "don't like" and much better than "hate" (in the reference). Thanks.
Pascal Thivent
+4  A: 

A simple questions such as, What is JRE and JDK.? Why does java claim interoperability? though these are very basic still many developers do no know What is I suggest these are more important to know before heading for the code related queries.

Ravisha
Don't know about the JDK/JRE part. For instance, there is no such distinction on the Mac. So as a Java developer on the Mac, there really is no need at all to know about that.
Fabian Steeg
I think this is a fine question, and if it helps expose people who are misguided enough to attempt serious Java development on a Mac all the better. Seriously, if you claim to be a Java developer but wouldn't know which download link to click on java.sun.com then there's a problem.
mbaird
Well being a java developer one need not worry at all about the platform he/she s woring on.understanding the java tools seems basic requirement.Atleast i consider so.i am not aware about Mac OS.still i do not m=worry much about OS in which i work.
Ravisha
+6  A: 

What is 'System', 'out', 'println' in System.out.println ? What happens when you call 'put' on HashMap ?

Adi
+1  A: 

How about what is a session bean and describe some differences between stateless and stateful session beans.

Kristian
+4  A: 

If you are hiring graduates with Java "experience" a simple question like Write some code that will cause a NullPointerException to be thrown can distinguish which candidates have used Java recently, and didn't just stop when they finished their unit/course.

David
Is "throw new NullPointerException()" a correct answer?
Thorbjørn Ravn Andersen
Well, it's up to the interviewer. If they showed an example of testing a variable and throwing a NPE with a helpful message then I'd be happy. If I thought they used that answer because they couldn't come up with some code that would legitimately throw an NPE then I'd be worried.
David
+11  A: 

One sure is comparison of string. Difference between

String helloWorld = "Hello World";
helloWorld == "Hello World";
"Hello World".equals(helloWorld);

zapping
Pretty sure you'd need to do something like new String("Hello World"). Aren't string constants pulled from a pool, much like the effect intern() would have, therefore making both statements true?
Steven Schlansker
http://javatechniques.com/blog/string-equality-and-interning/
zapping
+5  A: 
  1. Explain the various access modifiers used in Java. I have had lots of people struggle with this, especially default access.
  2. If you could change one thing about the Java language or platform what would it be? Good developers will have an answer here while those who aren't really interested in development probably don't care.
  3. If their CV says something like they use EJB2.1 then ask about EJB3 to see what they know about it. The best developers will keep up with the latest developments even if they don't use the newer versions.
Mark
If there was just _one_ thing I could change, I'd add operator overloading to the language, probably with just a restricted set of overloadable operators (i.e., no overloading `new` or `^` or `>>`, `<<` and `>>>`). If I could change another thing, that would be to add _true_ call-by-reference semantics. A third thing would be to add `unsigned` integral types.
Chinmay Kanchi
delegates and closures and maybe a lambda expression, but that's not really necessary. Extension Methods would be grand as well. They really can clean code up if used properly.
Joel
+3  A: 

"What's a deployment descriptor?"

If the candidate shudders involountarily, he has experience working with pre-3.0 EJBs.

Michael Borgwardt
oh man... I feel the pain!
Tim Drisdelle
Elister
+16  A: 

Can an interface extend multiple interfaces?

Most people answer "no", because they know java doesn't have multiple inheritance. But an interface can still extend multiple interfaces (but a class can't extend multiple classes). This doesn't lead to the diamond problem.

If the answer is "no", the interviewer should ask "why would it be forbidden?". Then you start thinking about it and you should realize that there is not problem with it.

So you learned something (by yourself) in the interview and you showed the interviewer that you are able to reason about classes, objects, inheritance, polymorphism, etc. It's actually much better than a candidate who knows the answer by heart but doesn't understand why

ewernli
I confused a C++ lecturer yesterday with the diamond inheritance problem, she didn't know about it at all :)
Esko
I was once asked this question and i answered "no". I think it was stress plus i answered too fast. its always hard to know answers to things you never use.
01
I elaborate a bit more in the text. Answering "no" is actually ok. What is nice with this question is that it's good way to trigger a discussion about OO concepts ans see how the candidate reasons. If the interviewer you had just wanted to hear "yes" then it's effectively pointless. Multiple inheritance of interfaces is indeed not that common to be known by heart.
ewernli
+52  A: 
BalusC
Personally I'd never expect someone to know that Calendar uses lazy initialization, because it requires you to look at the source to know that fact. It's not visible via the API.
Joachim Sauer
Read section "Field Manipulation" of Calendar API introduction: http://java.sun.com/javase/6/docs/api/java/util/Calendar.html *Although calendar field f is changed immediately, the calendar's time value in milliseconds is not recomputed until the next call to get(), getTime(), getTimeInMillis(), add(), or roll() is made.*
BalusC
Oh, I was wrong. I still wouldn't expect anyone to know this ;-)
Joachim Sauer
I wouldn't expect anyone to remember much of anything about Calendar except that it's *horrible*. An instance of Calendar doesn't even represent a calendar. If it can't even get that right... sigh.
Laurence Gonsalves
You don't need to name them all :) Just at least five (different) patterns is enough to testify the good JEE developer.
BalusC
Some good questions, but not this one: "Which design patterns do you spot in the java.util API itself? Where exactly? (name at least 5)". Don't expect me to answer this unless you provide source for java.util.*.
GreenieMeanie
Then Java EE is maybe not for you yet.
BalusC
Hm, some of these are rather detailed questions which you could easily look up. I feel they measure familiarity with J2EE more than general Java and OO dev experience. But maybe that's what you want...
sleske
- "Which design patterns do you spot ?"- Jim, I am a programmer, not a zoologist...
KarlP
@sleske: you aren't allowed to lookup during a personal interview :) Of course you don't need to answer *exactly* that as above. Just the degree how much the actual answer matches the abovegiven answers indicates the skillness of the Java EE programmer in [email protected]: Those are all fairly trivial though. In (high-degree) Java EE it's pretty important to recognize and understand some basic patterns, because it proves that you **know** and **understand** how and where to use/apply them in (customized) Java EE code.
BalusC
@BalusC: OK, if you judge the answers like that, I can agree. I'm just wary of testing trivia knowledge rather than experience and problem solving ability.
sleske
@sleske: I would like to do that as well, however, asking questions targeted on experience and problem solving ability may yield very varying subjective answers. You can't sum that up in just an answer at SO ;) Anyway, you have mocks and mocks and interviews and interviews.
BalusC
"Just the degree how much the actual answer matches the abovegiven answers indicates the skillness of the Java EE programmer in question"I disagree. It indicates the *knowledge* of the programmer, not the skill. We once hired a contractor that could rattle off all of this, answered all the interview questions well, but couldn't program his way out of a paper bag when it came down to it.For me, the answers were all obvious *as I read them* - I knew these things - but I would have a very hard time answering them in an interview setting.
Stephen P
@Stephen P: I think I made a poor word choice with "skill". Sorry about that, English is not my native. But you're entirely right, **practical experience** weights much more.
BalusC
@BalusC - I was reading the questions and answers, was amazed was thinking about who could have written in such a detailed manner, No surprise when i saw your name below.Thanks [email protected] - It is midnight here now and the town is very silent but I laughed very loudly after reading your comment. ;) very funny. :D
gurupriyan.e
+2  A: 
  • What is the general contract when overriding equals?
  • Is better option prefer lists or arrays?
  • What are the generally accepted naming conventions?
  • How serialization works?
  • How to implement Comparable?
  • What are the advantages of using JDBC's Prepared Statements?
  • What is Java EE?
  • What is a container and what services does it provide?
JuanZe
+1  A: 

Difference between and web server and a web container

NS
+4  A: 

You said "Good","Developer". Here are my 2 cents too.. :)

  • What does a "checked exception" mean?
  • Which one is better to use and when: Assertions or Exceptions to handle unexpected conditions?
  • Why String class is final? (or is it not? ;) )
  • are the wait, notify and notifyAll methods in Object class?
  • Why isn't Thread class final? Why would I extend Thread, ever?
  • Why there are two Date classes; one in java.util package and another in java.sql?
  • What happens if an exception is thrown in finally block? Is the remaining finally executed or not?
  • There is a garbage collector alright, but then is memory leak totally absent in a Java applications? If not, how so?

For J2EE:

  • Is it good to have instance/static variables in a servlet? Why not? Then where do you store "state"?
  • continuing on above question: what & where is a "state" for a (web) application?
  • What happens if I started creating/closing DB connections in "JSP"?
  • What are the ways to handle JSP exceptions? try-catch? Hmmm.. is there anything else?

I can think many, many, many more of 'em but this'll do for now :)

Elister
Answers would be nice, I don't know the answer to some of them.
MAK
Checked exceptions are those that a program has to have a plan to handle should they occur. If there is no try/catch block around something that throws a checked exception the program will not compile.wait, notify and notifyAll are for synchronising threads, and in Java all Objects can be synchronised on.I would extend Thread (as opposed to implementing Runnable) if conceptually the class requires its own thread, i.e. it needs to do some management, and does not represent a task that could be run on another worker thread.
ZoFreX
If an exception is thrown in a finally block then the rest of it will not run.In a "pure" Java program, canonical memory leaks do not exist. However, memory could still get used that shouldn't if data structures contain references to objects that are no longer needed. If using native libraries then their resources may need to be managed, for example in SWT it is important to call .dispose() on certain assets to make sure they do not leak.
ZoFreX
A: 

How do threads work? What is synchronized? If there are two synchronized methods in a class can they be simultaneously executed by two threads. You will be surprised to hear many people answer yes. Then all thread related question, e.g. deadlock, starvation etc.

fastcodejava
That is because the answer ´is´ yes...
Fortega
@Fortega - How can that be? Each method has a lock on the object. If one `thread` is running the method, no other `thread` can obtain the lock.
fastcodejava
If you have two objects of the same type, you can call the first method on the first object, and the second method on the second object simultaneously. Unless the methods are static of course.
Fortega
@Fortega - That's a no brainer. You can call the same method simultaneously if it is on a different instance.
fastcodejava
If you don't understand properly how the locking takes place, that's not really a no brainer. Also, although it might be a no brainer, that does not mean my answer was incorrect :-)
Fortega
+2  A: 

What do you like most / least about Java and why?

Carsten
+3  A: 

What is the difference between an abstract class and an interface? When would you use each of them?

Lots of Java developers don't know this, I asked most people on my computer science course at university and the vast majority could not answer it.

ZoFreX
The only difference is abstract class can contains non-abstract methods.
TBH
That's the answer you'd have if you'd read a Java book, sure. Although then you might also add that they can contain member variables, too :P But the difference in when to use them, and why, is even more interesting.
ZoFreX
+2  A: 

why would you override the toString() method?

SWD
+1  A: 

What is the difference between J2SE and J2EE (or JSE and JEE)?

A developer should be able to point out that the enterprise edition is basically an interface definition (i.e. a specification) which can be implemented by vendors. Whereas the standard edition is an implementation in its own right

oxbow_lakes
Is the ability to keep marketing terminology straight really that important at the end of the day?
Laurence Gonsalves
@Laurence - that's a ridiculous thing to say. There is a real difference between the two things which you'd be amazed how many supposedly experienced developers just don't understand
oxbow_lakes
My point is that if someone is a good coder, I couldn't care less if they know the difference between J2SE and J2EE.
Laurence Gonsalves
I read the question as being "stuff that a JEE+JSE developer should know", in which case I think that understanding the difference between the two technologies is relevant.
oxbow_lakes
@Laurence, I would have a very hard time imagining a _good_ JEE coder which did not know the difference between EE and SE.
Thorbjørn Ravn Andersen
@Thorbjørn Ravn Andersen I doubt that most Java coders, good ones included, are strongly aware of the distinction above, "the enterprise edition is basically an interface definition ... Whereas the standard edition is an implementation in its own right". To most good Java coders the distinction that matters is which APIs are available. eg: Servlets are part of EE, etc. In any case, just as I think it's more important to find good programmers than good language-X programmers, I think it's more important to find good Java programmers than good JSE/JEE programmers.
Laurence Gonsalves
@Laurence. If the person in question is to have anything to do with a JEE system, I would strongly expect them to know this (not as much if they are not). Perhaps we just have different expectations of what a "good" programmer is.
Thorbjørn Ravn Andersen
+1  A: 

How does volatile affect code optimization by compiler?

Roman
That entry should contain the correct answer too.
Thorbjørn Ravn Andersen
Roman
+1  A: 

What is difference between String, StringBuffer and StringBuilder?

Harsha
A: 

When would you use Session Beans or Message Driven Beans ?

How are transactions handled in session beans ?

What is the difference between local and remote session beans ? This question is more about knowing that RPCs are in the picture or not, since a method that is exposed as both Local or Remote might still work differently (side-effects on parameters are possible with Locals, while not possible with Remotes).

A good test is: never ask the questions in the same order. We had the experience with offshoring that sometimes they were actually giving the response in the wrong order :-). As a result you should make sure that you can actually see the person you are interrogating.

David Nouls
+1  A: 

A more pure Java question:

What is the difference between sleep and wait ? Not many people actually understand how wait is working.

How do you need to handle InterruptedExceptions ?

David Nouls
A: 

Q. Give a real world example scenario where you will use GenericServlet not HttpServlet ?

akjain
Can you give me an example please?
harigm
I don't have an exact answer for this, but basically if you have to write a servlet which can handle non-HHTP requests from a client then, is it possible to do it with a GenericServlet implementation ?
akjain
+1  A: 

Write a program to accept two integers and output the largest of two numbers to a file at a location of your choice. Now describe what each statement does.

It's possible to drill down pretty deep starting from the significance of the import statement, right down to abnormal termination

Everyone
+1  A: 

Java/J2EE Interview Questions and Answers have some very useful ones.

Sarah Nasir
A: 

What will be printed?

public void testFinally(){
    System.out.println(setOne().toString());

}

protected StringBuilder setOne(){
    StringBuilder builder=new StringBuilder();
    try{
        builder.append("Cool");
        return builder.append("Return");
    }finally{
        builder.append("+1");
    }
}

Answer: CoolReturn+1

A bit more difficult:

public void testFinally(){
    System.out.println(setOne().toString());

}

protected StringBuilder setOne(){
    StringBuilder builder=new StringBuilder();
    try{
        builder.append("Cool");
        return builder.append("Return");
    }finally{
        builder=null;  /* ;) */
    }
}

Answer: CoolReturn

zaletniy
+1  A: 

Core: 1. What are checked and unchecked exceptions ? 2. While adding new exception in code what type (Checked/Unchecked) to use when ?

Servlet: 1. What is the difference between response.sendRedirect() and request.forward() ?

YoK
+1  A: 

One thing many Java programmers don't know is that Strings are immutable, so use StringBuilder or StringBuffer!

String s = "";
for(int i = 0; i < 100; i++) {
  s += "Strings " + "are " + "immutable, " + " so use StringBuilder/StringBuffer to reduce memory footprint";
}
Shervin
@Shervin And Number's such as Integer, Long and so on
Arthur Ronald F D Garcia