Multiple choice

What will be returned by the function capacity()?

StringBuffer s = new StringBuffer(“Hello”); s.capacity();

  1. 16

  2. 4

  3. 18

  4. 3

  5. 15

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The correct answer is 16. When stringbuffer object is created the default capacity of the object is always 16. If the number of characters in the string increases the capacity will increase with the following formula- 2 * (previous capacity + 1). In this case hello has 5 characters, so default capacity will not change.