Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be output of the following program?
public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer buff = new StringBuffer("Oh! Java Programming");
        System.out.println("Buffer = " + buff);
        char[] array = new char[]{'M', 'e', 'r', 'i', 't', ' ', 'C', 'a', 'm', 'p', 'u', 's'};
        buff.getChars(2, array.length, array, 3);

A. Buffer = Oh! Java Programming
Merit Campus
B. Buffer = Oh! Java Programming
! Java Programming
C. Buffer = Oh! Java Programming
Mer! Java Pro
D. Compilation Error or Runtime Error
Topic: StringBuffer getChars() Method In Java 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.

Answer C we will take the chars from buff starting at loc 2 and overrite the char array from loc 3

Posted by Deepak Kumar Yadav    2014-08-19 12:42:05

Ans is D.ArrayIndexOutOfBounds Exception arises. The destination index is the size of the array which is not a valid index(size -1 )

Posted by Nikhil Sulibhavi    2014-08-19 12:45:19

c.getchar method copies characters from this string into the destination character array..
here 2=index of the first character in the string to copy.
arrau.length=12 is th indx til wen to bb copied.
array is the dstntn for pastng  the strng..
3 us locatn in array where thw strng to b pastd
hence ! java pro is copied n pasted in array strng

Posted by Asma Mujtaba Khan    2014-08-19 12:46:01

D is ans.. it gives arrayindexoutbound exception.. we are trying to copy the string in string buffer..its size is more than the array size.. so it gives exception...

Posted by Sareesh Rayapati    2014-08-19 12:53:10

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-08-20 05:02:03

© meritcampus 2019

All Rights Reserved.

Open In App