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

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

Moderator: admin

Post Reply
bari2009
Posts: 3
Joined: Sat Jul 15, 2017 1:22 pm
Contact:

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

Post by bari2009 »

This question asks about emptying the contents of a StringBuilder called sb. The answer I chose is "wrong", but I tried the code and it works:
sb.delete(0,sb.length());

The "right" answer is:
sb.clear();

Why isn't sb.delete(0,sb.length()) correct also?

I appreciate any clarification on this. Thank you so much!

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

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

Post by admin »

I see that option 1 i.e. sb.delete(0,sb.length()) is indeed set as the correct answer. sb.clear() is incorrect.
If you like our products and services, please help us by posting your review here.

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson »

It's really wierd that delete() method doesn't throw an IndexOutOfBound error when we pass a value exceeding length of characters in StringBuilder. Like sb.delete(0,10000000) //compiles and runs without any issues..

tosidis
Posts: 3
Joined: Fri Oct 26, 2018 4:45 am
Contact:

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

Post by tosidis »

I think that clear() is for ArrayList and for the StringBuilder you can use delete()

Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests