Menu
Topics Index
...
`

Creating StringBuffer and using its methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating StringBuffer and using its methods
What is the output of the program?
public class DailyDoseB {
    public static String overlay(String str, String overlay, int start, int end) {
        if (str == null) {    return null;    }
        if (overlay == null) {    overlay = "";    }
        int len = str.length();
        if (start < 0) {    start = 0;    }
        if (start > len) {    start = len;    }
        if (end < 0) {    end = 0;    }
        if (end > len) {    end = len;    }
        if (start > end) {
            int temp = start;
            start = end;
            end = temp;
        }
        return new StringBuffer(len + start - end + overlay.length() + 1).append(str.substring(0, start)).append(overlay).append(str.substring(end)).toString();
    }
    public static void main(String[] arg) {
        System.out.println(overlay("abcdef", "xxx", 1, 4));
    }
}

A. bcde
B. axxde
C. axcde
D. axxxef
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java substring

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.

ans is D. i.e., axxxef. A string buffer of size 7 (6 +1 -4 +3 +1) is created and to that a is appended (str.substring(0,start)) and overlay i.e., xxx is appended and at last substring of str from 4th position (end value) till last is appended and converted to string.

Posted by Nikhil Sulibhavi    2014-03-06 13:23:05


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

Posted by Merit Campus    2014-03-07 04:57:45


© meritcampus 2019

All Rights Reserved.

Open In App