About Question enthuware.ocajp.i.v8.2.1445 :

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

Moderator: admin

Post Reply
vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

About Question enthuware.ocajp.i.v8.2.1445 :

Post by vlezz94 »

Hi Paul!

I have a little doubt in the following question:
Assuming that the following method will always be called with a phone number in the format ddd-ddd-dddd (where d stands for a digit), what can be inserted at //1 so that it will return a String containing "xxx-xxx-"+dddd, where dddd represents the same four digits in the original number?

Code: Select all

public static String hidePhone(String fullPhoneNumber){ 
  //1 Insert code here }
This option is marked as correct:

Code: Select all

return new StringBuilder("xxx-xxx-")+fullPhoneNumber.substring(8); 
Can I just assume that substring returns an String and that's why the hidePhone method it's accepting it?

Because at first I thought that it would throw an exception, because you can't pass an StringBuilder to a method that is asking for a String.

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

Re: About Question enthuware.ocajp.i.v8.2.1445 :

Post by admin »

Yes, fullPhoneNumber.substring(8) returns a String. The + operator basically just converts the other object to String (using that object's toString method) if one operator is already a String.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1445 :

Post by Rinkesh »

Hi Paul,just a request that please try to add examples to explanation if possible for StringBuilder Methods!

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

Re: About Question enthuware.ocajp.i.v8.2.1445 :

Post by admin »

Sure, will add more examples. But one example is already there in the option new StringBuilder("xxx-xxx-")+fullPhoneNumber.substring(8); Here the first operand is not a String and the second operand is a String. So the non-string operand is converted to String using its toString method and then concatenated with the string operand.

String concatenation is covered in other questions as well, where you will see many more examples.
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1445 :

Post by baichen7788 »

Hi
Is CharSequence same as String ?

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

Re: About Question enthuware.ocajp.i.v8.2.1445 :

Post by admin »

No, CharSequence is not the same as String. CharSequence is an interface. String, StringBuilder, and StringBuffer implement it. Please see this:
https://docs.oracle.com/javase/8/docs/a ... uence.html
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests