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

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

Moderator: admin

Post Reply
baptize
Posts: 32
Joined: Wed Mar 14, 2012 5:45 pm
Contact:

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

Post by baptize »

see attached photo, careful when minimum capacity is negative, this is tricky.

Just thought of sharing it with you guys. :D
Attachments
Screen Shot 2013-03-15 at 12.21.19 PM.png
Screen Shot 2013-03-15 at 12.21.19 PM.png (38.13 KiB) Viewed 9865 times

terence_j_coffey
Posts: 2
Joined: Wed Jun 05, 2013 6:01 am
Contact:

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

Post by terence_j_coffey »

Working with java data types

StringBuffer
Notes state "If the minimum capacity argument is non positive this method takes
no action and simply returns."
I've tried this and I observe the following

If I enter StringBuffer sba = new StringBuffer(-5); it compiles fine but i get a runtime error.
Exception in thread "main" java.lang.NegativeArraySizeException
at java.lang.AbstractStringBuilder.<init>(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at InitClass3.main(InitClass3.java:54)
I'm using java 1.7.0_17
If I create a StringBuilder string with initial capacity of zero, there is no runtime error.
I've just added this in with other code but the error message is clear where the issue is.



fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

As the question asked "at least 100" (minimum), how "StringBuilder sb = new StringBuilder(100);" is correct?
Doesn't A option specified certain number of characters? or it defined the minimum number of characters and it could be more during coding?

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

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

Post by admin »

Because capacity of 100 satisfies what the question asks i.e. at least 100. I am not sure what is your doubt.
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

I just assumed as the question asked "at least 100" (minimum) which means capacity 100 character is minimum and the variable could have more than 100 characters, but "StringBuilder sb = new StringBuilder(100);" has exact capacity not more.
Am I wrong?

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

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

Post by admin »

If you create a StringBuffer with some capacity (or even without specifying capacity), it will always be created with a certain capacity.
Creating a StringBuffer with 100 doesn't mean it can store only 100 characters. It can store more if required. But initial capacity is 100, which satisfies the requirement given in the question.
If you like our products and services, please help us by posting your review here.

kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post by kashyapa »

i got little bit confuse with the explanation.
The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.
:roll:

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

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

Post by admin »

Not sure what is confusing about it. You have two numbers. Just pick the larger out of those two.
If you like our products and services, please help us by posting your review here.

Smoljanov Sergej
Posts: 4
Joined: Mon Sep 15, 2014 8:24 am
Contact:

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

Post by Smoljanov Sergej »

kashyapa wrote:i got little bit confuse with the explanation.
The new capacity is the larger of:
The minimumCapacity argument.
Twice the old capacity, plus 2.
:roll:

Code: Select all

         StringBuilder sb = new StringBuilder();
         System.out.println("new StringBuilder().capacity() = " + sb.capacity());
         //capacity of new StringBuilder() is 16
         sb.ensureCapacity(30); 
         // if current (currentCapacity*2+2)> argumetn of ensureCapacity()
         // result will be (currentCapacity*2+2)
         System.out.println(sb.capacity());
         sb = new StringBuilder();
         sb.ensureCapacity(34); 
         System.out.println(sb.capacity());
         sb = new StringBuilder();
         sb.ensureCapacity(35); 
         System.out.println(sb.capacity());
         // if current (currentCapacity*2+2)< argumetn of ensureCapacity()
         // result will be argumetn of ensureCapacity()

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Great explanation @Smoljanov Sergej, thank you.

I dint get this,
Observe that the question says "at least 100 characters". In the exam, you may get a question that says "100 characters", in that case, ensureCapacity() may not be a valid option.
Why would ensureCapacity() is not a valid option when question says "100 characters"?

Thanks,
GPAR

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

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

Post by admin »

Because the method may allocate more than 100. Basically, you need to be careful about the wordings used in the question.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Thanks for the explanation.

Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests