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

Moderator: admin

Post Reply
JavaCoffee
Posts: 3
Joined: Tue Jul 29, 2014 10:47 am
Contact:

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

Post by JavaCoffee » Tue Jul 29, 2014 10:48 am

Hi,

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

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Jul 29, 2014 10:54 am

Yes, it is correct. Can you please tell me why do you think it is not?
-Paul.
If you like our products and services, please help us by posting your review here.

JavaCoffee
Posts: 3
Joined: Tue Jul 29, 2014 10:47 am
Contact:

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

Post by JavaCoffee » Tue Jul 29, 2014 12:00 pm

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!

yuneedto
Posts: 2
Joined: Tue Jan 19, 2016 11:49 am
Contact:

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

Post by yuneedto » Tue Jan 19, 2016 12:03 pm

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

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Jan 19, 2016 9:22 pm

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.
If you like our products and services, please help us by posting your review here.

yuneedto
Posts: 2
Joined: Tue Jan 19, 2016 11:49 am
Contact:

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

Post by yuneedto » Tue Jan 19, 2016 11:13 pm

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.

caseyfried
Posts: 1
Joined: Sun Apr 03, 2016 12:08 pm
Contact:

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

Post by caseyfried » Sun Apr 03, 2016 8:46 pm

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.

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Apr 03, 2016 10:24 pm

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.
If you like our products and services, please help us by posting your review here.

philfrei
Posts: 3
Joined: Fri Apr 05, 2013 5:25 pm
Contact:

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

Post by philfrei » Wed Jul 20, 2016 12:14 am

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));

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Jul 20, 2016 11:13 am

No, the given answer is correct. You need to define b1 as StringBuilder, not String.
If you like our products and services, please help us by posting your review here.

ankitkrsingh
Posts: 1
Joined: Sat Jan 21, 2017 8:36 am
Contact:

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

Post by ankitkrsingh » Sat Jan 21, 2017 8:44 am

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

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Jan 21, 2017 11:22 am

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"));
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests