Topics Index

String buffer test

If you need explanation Read this topic

If you need Answer Take test on this topic

String buffer test
What will be the output of the following program?
public class GoodEvening {
    public static void stringReplace(String text) {
        text = text.replace('j', 'c');
    public static void bufferReplace(StringBuffer text) {
        text = text.append("c");
    public static void main(String args[]) {
        String textString = new String("java");
        StringBuffer textBuffer = new StringBuffer("java");
        System.out.println(textString + textBuffer);

A. javajavac
B. cavajavac
C. javac
D. java
E. Compilation Error or Runtime Error
Topic: Java StringBuffer replace() 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 Ramkumar Boggarapu    2013-05-22 12:39:48

because in java 'j' replaced by 'c' and in text buffer c is appended to java it becomes javac then + concats two strings result is cavajavac

Posted by Ramkumar Boggarapu    2013-05-22 12:41:40

option a is answer because strings wont change the string but string buffers are changeable so j not replaced in string but appends in string buffer

Posted by Sai Lalitha Renduchintala    2013-05-22 13:18:19

strings wont change but replace function  Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

Posted by Ramkumar Boggarapu    2013-05-22 13:37:43

Congratulations Sai Lalitha. You are today's daily dose winner. Please send us your mobile details and we will recharge tomorrow after considering the new likes.

Posted by Merit Campus    2013-05-22 14:21:18

thanQ merit campus got rc :)

Posted by Sai Lalitha Renduchintala    2013-05-31 12:23:31

© meritcampus 2019

All Rights Reserved.

Open In App