Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Jul 29, 2014 10:48 am
by JavaCoffee
Hi,

Are you sure about the answer ? I'd say it isn't correct. Can you confirm this is the right answer?

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Jul 29, 2014 10:54 am
by admin
Yes, it is correct. Can you please tell me why do you think it is not?
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Jul 29, 2014 12:00 pm
by JavaCoffee
I just realized that the statements are : "independ of each other"... I was making it more difficult than it was. I first thought it was linked to each others.
Sorry and thanks for your quick reply!

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Jan 19, 2016 12:03 pm
by yuneedto
StringBuilder b2 = new StringBuilder("yoodler");
The return value of b2.substring(2,5) is odl.

If StringBuilder will modify the object. Why the b2 doesn't change? Hasn't it changed already?

How should I realize these operation works ?
>>> b2.substring(2,5).toUpperCase()

Thank you for your reading ,patience and wisdom. Please tell me why.
Sincerely,Yu

-----------------------------------------------------------------------------------------------------------------------------------------------
StringBuilder b1 = new StringBuilder("snorkler");
StringBuilder b2 = new StringBuilder("yoodler");

b1.append(b2.substring(2,5).toUpperCase());
System.out.println(b1);
System.out.println(b2);

b1:snorklerODL
b2:yoodler

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Jan 19, 2016 9:22 pm
by admin
yuneedto wrote:StringBuilder b2 = new StringBuilder("yoodler");
The return value of b2.substring(2,5) is odl.

If StringBuilder will modify the object. Why the b2 doesn't change? Hasn't it changed already?
Not every method of StringBuilder will change the StringBuilder object. Here, b2.substring(2,5) returns a new String object. It doesn't modify the original StringBuilder object.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Jan 19, 2016 11:13 pm
by yuneedto
Wow,Thank you so much.
Because I can't find answers of my question on internet. I think I misunderstood the method. It gets so clear by your answers. You are a great teacher. Really appreciate for your help.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Sun Apr 03, 2016 8:46 pm
by caseyfried
I can't find any information online or in my text that says that a StringBuilder object can use String methods, i.e. substring and replace. Could you please provide me with a reference that lists all the methods that Stringbuilder can use?

Is the rule that you can use the String methods, but they won't change the Stringbuilder like the Stringbuilder methods will. The only StringBuilder methods I can find are append, delete, insert, reverse, and toString.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Sun Apr 03, 2016 10:24 pm
by admin
No, there is no such rule that StringBuilder can use String methods. A class can use only those methods that it has (i.e. either implemented itself or inherited).

I think you are confused by methods of same signature existing in String as well as in StringBuilder. But whether any other class has the same method is irrelevant. You should just see if the class has that method or not by looking at the JavaDoc description. In this case: https://docs.oracle.com/javase/7/docs/a ... ilder.html It shows that it does have substring and replace methods. Therefore, you can use them.

Also, which text are you referring to when you say you don't see it in your text?
Always refer to official JavaDoc.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Wed Jul 20, 2016 12:14 am
by philfrei
The last option threw me, the part that ends with "append(false)".

When I put in the following code, I get a compiler error, saying that the argument of append() cannot be a boolean. I'm thinking the word "false" should be enclosed in quotes, yes?

Code: Select all

		String b1 = "yoodler";
		System.out.println(b1.append(false));

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Wed Jul 20, 2016 11:13 am
by admin
No, the given answer is correct. You need to define b1 as StringBuilder, not String.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Sat Jan 21, 2017 8:44 am
by ankitkrsingh
Foundation Test question no -
I have different answers . Please check where I am wrong

StringBuilder b1 = new StringBuilder("snorkler"); //b1=snorkler
StringBuilder b2 = new StringBuilder("yoodler"); //b2=yoodler
b1.append(b2.substring(2, 5).toUpperCase()); //b1=snorklerODL, b2=yoodler
b2.insert(3, b1.append("a")); //b1=snorklerODLa, b2=yoosnorklerODLadler
b1.replace(3, 4, b2.substring(4)).append(b2.append(false)); //b1=snonorklerODLadlerklerODLayoosnorklerODLadlerfalse, b2=yoosnorklerODLadlerfalse

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Sat Jan 21, 2017 11:22 am
by admin
As the problem statement says, the statements given in the first column are to be executed independent of each other. Thus, for example, in case of the second row, you should execute:

StringBuilder b1 = new StringBuilder("snorkler"); //b1=snorkler
StringBuilder b2 = new StringBuilder("yoodler"); //b2=yoodler

b2.insert(3, b1.append("a"));

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Sep 29, 2020 12:15 pm
by plboronski
I'm not understanding the very last statement. Here is my thought process, please let me know where I'm going wrong:

b1.replace(3,4,b2.substring(4)) //b2.substring(4) should be the letter "l", and since the replace method's end index is non-inclusive,
//the third element of b1 should be set to "l" which would produce "snolkler"
.append(b2.append(false)); //given the above, this should append "yoodlerfalse" to "snolkler" producing "snolkleryoodlerfalse"


The practice test says "snolerkleryoodlerfalse" is the correct answer and I'm not seeing how that is produced. Any insights would be greatly appreciated.

Re: About Question enthuware.ocajp.i.v7.2.1184 :

Posted: Tue Sep 29, 2020 12:24 pm
by plboronski
Never mind, I figured it out. Turns out that when substring is called with only one index, it returns the substring starting at that index and ending at the end of the string, so in this case b2.substring(4) returns "ler" not "l" as I originally thought.