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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

plboronski
Posts: 2
Joined: Tue Sep 29, 2020 12:05 pm
Contact:

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

Post 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.

plboronski
Posts: 2
Joined: Tue Sep 29, 2020 12:05 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 35 guests