Q: Error in OCJP exam simulation: how many objects are really created? In a simulation for a the OCJP certification I found this question:
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");

How many objects are created ?

They state that the correct answer is 4 because they state:
s1 is one object, s2 is another object, 
s3 is another object and "abc" is another String Object .

But for me it's wrong and it should be 3, because s1 and s2 are the same object. What do you think?

A: You are right that the answer is not 4 objects.
However, the question "how many objects are created" is ambiguous.  The issue is that one of the three objects is not created when you execute the code.  Specifically, the String object that corresponds to the "abc" literal is actually created when the code is loaded.  When that code is executed, two StringBuffer objects are created, and the pre-existing String object is used.
And in fact it is more complicated than that, since at class load time it is possible that another temporary String object is created and then discarded after it has been interned;


*

*If an "abc" literal has already been loaded in a different class, then that one will be used.  

*It is not specified if the string pool implementation makes a fresh copy of the String if it needs to put it into the pool.
Unless the question is more precisely stated, there is no single correct answer.  The best you can say is:


*

*Two StringBuffer objects are created when the code is run.

*One or two String objects are created when the code is loaded.


Then there is the issue of whether you should count the private char[] objects that form part of the StringBuffer and String objects.  That could inflate the object count to as much as 8.

A: Yes definitely 3 Object.both s1 and s2 referring same location. so s1, s2 and "abc" are the objects here. May be it is better not to follow that reference.  

A: CORRECTION
There should be 3 objects:
1. StringBuffer s1 = new StringBuffer("abc");

Two objects will be created in memory s1 & "abc" . This is because, strings are interned and literals are added to a memory pool.
2. StringBuffer s2 = s1;

No object will be created here because s2 will point to "abc" created as part of s1
3. StringBuffer s3 = new StringBuffer("abc");

Only one object will be create for s3.

A: How about:
1. StringBuffer s1 = new StringBuffer("abc");

1 builder object + 1 char[] object + (1 String literal, if created)
2. StringBuffer s2 = s1;

No new objects.
3. StringBuffer s3 = new StringBuffer("abc");

1 builder object + 1 char[] object
A StringBuilder encapsulates the backing char[] inside, which is an object.
As @StephenC says the question is ambiguous.

A: 3 or 4, depending on implementation.  Some compilers would create only one String object for the constant "abc" and reference it as many times as necessary, and other compilers would create one object per constant.  AFAIK, this has not been mandated by all the languages specification versions, and it could change again in the future.
Or more, depending on StringBuffer's implementation (which could actively create a char[] and copy the initialization String, as opposed to a lazy implementation that postpones this until the StringBuffer's contents are really changed).  Again, this should not be mandated by the language.  And, BTW, do arrays count as Objects for the purpose of the question?  And what if a StringBuffer implementation stored information in a JNI structure?  What that count as objects?  I'm just trying to re-inforce my point about non-language-mandated implementation details.
A test should not ask this kind of questions unless it is about a specific implementation and/or JLS version.
What is absolutely clear is that the reason they give for their answer of 4 is completely incorrect.  The assignment to s2 does not cause any new object to be created.
