Howdy! Errata is coming, by the way, SOON. And thanks to everyone who is helping put that together, but most especially Jessica and the, "doesn't miss ANYTHING and appears to have memorized the spec" John Paverd
For the exam, you need to know that IF you use 'new' to create a new String that is not *already* in the pool, TWO String objects are created. But there is no need to ever have duplicates in the String pool, so if the String is already in the pool at the time you use 'new', there will be only ONE created.
I will say that there is very little on the exam that deals with String pool issues. One issue that you *might* need to keep in mind, however, is that code like the following:
String s = "foo";
String g = s + "bar";
will imply that there are THREE rather than TWO String objects in the pool: "foo" (the first one); "bar" (the literal in the second statement), and "foobar" (the result of concatenating s + "bar").
What you will NOT see is the following variation:
String s = "foo";
String g = "bar";
String z = s + g + "baz";
Because NOW how many String objects are there? We don't know for sure whether there are really four or five. We DO know that there will be the following:
1) "foo"
2) "bar"
3) "baz"
4) "foobarbaz"
What we do NOT know is whether there will be an intermediate, "foobar", created by the concatenation of s+g. According to the spec, Java has the option of optimizing that away and not making/keeping that intermediate concatenation as a separate object.
But... how do we KNOW that those Strings aren't already in the pool because of some OTHER code?
On the exam, assume that the code you see is the ONLY code that matters. In other words, you do NOT have to speculate, "Hmmmmm... but what if some other code somewhere has already caused those String literals to be created?"
If you do not see it happening in the code example in the question, assume that it did not happen.
Which brings me to another point about the exam overall -- you ARE to assume that when a question is asked, everything that you do not see is working normally. In other words, if you see a question about assertion code, ASSUME that assertions are enabled properly. If you see a question that does not show a main method calling the method shown in the question, ASSUME that the method you see is called correctly... that somewhere the question is invoked properly.
We do this because
you should never be expected to imagine all the things that could go wrong, unless the things that can go wrong are part of the question. For example, it really would be ridiculous if you had to ask for each question, "OK, but can we assume the programmer has the path variable set correctly?" or even more absurd, "Are we to assume that the programmer has installed Java?" (otherwise, of course, NOTHING would work
So I guess my advice is this:
* Assume that everything is installed and working correctly, and that anything else related to the question works.
* Assume that if you are shown a method and asked for the result, that other code you do not see has correctly invoked that method.
* Assume that if a particular thing is important for the question, it will be part of the question or answer -- there are NO 'ambiguous' questions if you really understand what is being asked, and if you make the two assumptions above.
Of course there ARE times when you cannot make assumptions -- especially with garbage collection. For example, if you see a question that asks at what point an object becomes eligible, and yet the question shows that a reference to the object is passed into a method (a method that you do NOT see), you simply cannot answer that question with certainty, because you do not KNOW what the method is doing with that parameter (the reference it just received).
So not to give anything away here... but if *I* saw a question like that, hypothetically of course, I would probably not select any of the answers that say with CERTAINTY exactly what would happen.
My advice also is to become familiar with the
word "indeterminate"

as in, "impossible to determine from the information given".
(you'll see issues like this for both
thread and gc questions in particular)
And since I'm here, I might as well mention something that I've been getting a lot of questions about from confused readers -- the questions that say,
"Which two lines, inserted independently at line 16, would allow the code to compile?"
When you see a question like that, it means that "If you could pick only ONE of these lines that -- inserted at line 16 -- would allow the code to compile, which would you pick?"
And then answer it twice.
(gee Kathy, that just cleared it RIGHT up
OK, let me try again
It means that you cannot put any TWO of the answers together. You have to treat EACH individual answer like a true/false question, all by itself, without considering any of the other answers. Because sometimes, you might see a question like that and realize that if you inserted both, say, B AND C together, the code would compile. But that is not how you are allowed to answer those "which two inserted independently..." questions.
You must look at -- and evaluate -- each answer separately (i.e. independently).
(personally, I argued that we should not use this wording on the real exam, but I am not -- as we say here -- high enough on the totem pole to have won that battle
cheers,
Kathy
Errata coming soon... we promise!