Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class InsertMethod {
    public static void main(String args[]) {
        StringBuffer s = new StringBuffer("output of the following program?");
        s.insert(10, 'f');
        System.out.println("After insert char: " + s);
        String obj = new String("two");
        s.insert(11, obj);
        System.out.println("After insert obj: " + s);

A. After insert char: output of fthe following program?
After insert obj: output of twothe following program?
B. After insert char: output of fthe following program?
After insert obj: output of ftwothe following program?
C. Some other output
D. Compilation Error or Runtime Error
Topic: Java StringBuffer insert() 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.


Posted by Shubham Bansal    2015-01-21 08:34:12

My Guess is Option B.. execution Starts at main().. Now 1st StringBuffer Object is Created .. and in next Step by using One of the Overloaded method insert . is used insert char at position 10 .. then printed modified StringBuffer Object and

then in Next Step ..

String Object is Created (Not Normal Object) and in succeeding step.. another overloading method insert is used to insert that String Object (obj) at position 11 .. And .. Again printed the Modified value..

Posted by ?????????? ?????    2015-01-21 08:47:10

Ans is Option B... Execution starts from main() method after execution of first stmt it will create a StringBuffer variable  "s" initialized with the value "output of the following program?" after execution of second stmt it will insert the character 'f' in 10th position of the variable "s" so it will print the s value as "output of fthe following program?" StringBuffer is mutable object so it can automatically update the value of variable s after that we are creating String object with the variable name as "obj" and it is initialized with the value two and again we are inserting this string object obj=two in 11th position of StringBuffer variable so it will print s as "output of ftwothe following program?" so Option B.. is Ans

Posted by Bhagi Bhagyasri    2015-01-21 08:53:49

ans is B execution starts with main() method stringbuffer variable s is created insert(10,'f') method is used to  insert 'f' in  the 10 th position of the stringbuffer and it prints "After insert char: output of fthe following program?" string obj is created  and insert(11,obj)  is "two" is inserted into the 11th position it prints "after insert obj: output of ftwothe following program?"

Posted by Ramya Reddy Addulla    2015-01-21 12:05:43

Ans is we have StringBuffer concept which is an mutable class which means we can edit the data in the obejct once we created unlike to String in
String if  we modify the data than new Object will created..


here we create the instance for the StringBuffer class by passing the "output of the following program?"
insert()-->this method is used to insert the string at the specified position..
we are printing now so it will print
After insert char: output of fthe following program?
here we are calling the insert by passing the 10 and f so f is inserted at the 10th postion and stored in the s
next we are calling the insert by passing the String object and index so 'two" get inserted after the f which we inserted previously

and we are printing it will print
After insert obj: output of ftwothe following program?

Posted by Uday Kumar    2015-01-21 15:57:03

This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', Bhagi Bhagyasri, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-22 08:22:22

© meritcampus 2019

All Rights Reserved.

Open In App