About Question enthuware.ocajp.i.v7.2.1184 :
Moderator: admin
-
- Posts: 3
- Joined: Tue Jul 29, 2014 10:47 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1184 :
Hi,
Are you sure about the answer ? I'd say it isn't correct. Can you confirm this is the right answer?
Are you sure about the answer ? I'd say it isn't correct. Can you confirm this is the right answer?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
Yes, it is correct. Can you please tell me why do you think it is not?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Tue Jul 29, 2014 10:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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!
Sorry and thanks for your quick reply!
-
- Posts: 2
- Joined: Tue Jan 19, 2016 11:49 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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
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
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.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?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Jan 19, 2016 11:49 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.
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.
-
- Posts: 1
- Joined: Sun Apr 03, 2016 12:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.
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.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.
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.
-
- Posts: 3
- Joined: Fri Apr 05, 2013 5:25 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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?
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));
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.
-
- Posts: 1
- Joined: Sat Jan 21, 2017 8:36 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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
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
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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"));
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.
-
- Posts: 2
- Joined: Tue Sep 29, 2020 12:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.
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.
-
- Posts: 2
- Joined: Tue Sep 29, 2020 12:05 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1184 :
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.
Who is online
Users browsing this forum: No registered users and 45 guests