Menu
Topics Index
...
`

Strings


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings
What will be the output of the following program?
public class Offers {
    public static void main(String[] args) {
        StringBuffer s1 = new StringBuffer(5);
        System.out.print(s1.capacity() + " ");
        s1.ensureCapacity(8);
        System.out.print(s1.capacity() + " ");
        s1.ensureCapacity(30);
        System.out.print(s1.capacity() + " ");
        s1.setLength(s1.length() + 10);
        System.out.print(s1.length());
    }
}

A. 5 8 30 15
B. 5 12 30 10
C. 0 8 30 10
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java StringBuffer ensureCapacity() Method With Example

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

option B as Stringbuffer intialized with 5 it has capacity of 5 to ensure 8 which is more than its capacity.Capacity increases 2(n+1) i.e to 12
To ensure 30 which is greater than 2(n+1)
it increases size to 30
length of string buffer is zero , so 0+10=10
output will be 5 12 30 10

Posted by Sasikanth Sharma    2014-07-17 03:47:26


B.....the capacity increaseas in the basis of 2(n+1)

Posted by Arul Palanisamy    2014-07-17 04:02:21


B

Posted by Gaurav Fulse    2014-07-17 05:06:54


Congratulations Sasikanth Sharma. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-18 06:45:09


© meritcampus 2019

All Rights Reserved.

Open In App