Multiple choice technology programming languages

What will be the ouput of the following code snippet? StringBuffer myBuffer = new StringBuffer(15); myBuffer.append("Hello World!"); System.out.println(myBuffer.length()+","+ myBuffer.capacity());

  1. 15,15

  2. 12,12

  3. 12,15

  4. 15,12

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

StringBuffer is initialized with capacity 15, but capacity is the allocated space, not the actual content length. The string "Hello World!" contains 12 characters (including space and exclamation). The length() method returns 12 (actual characters), while capacity() returns 15 (allocated space). Therefore, the output is "12,15".