Menu
Topics Index
...
`

Strings copyValueOf and getChars


If you need explanation Read this topic

If you need Answer Take test on this topic

Strings copyValueOf and getChars
What will be the output of the following program?
public class IntelMotherBoard {
    public static void main(String a[]) {
        char array[] = {'M', 'y', '_', 'J', 'a', 'v', 'a', '_', 'e', 'x', 'a', 'm', 'p', 'l', 'e'};
        String str = String.copyValueOf(array);
        System.out.print(str + ", ");
        String subStr = String.copyValueOf(array, 3, 4);
        System.out.print(subStr + ", ");
        subStr = String.copyValueOf(array, 3, 6);
        System.out.println(subStr);
        str.getChars(5, 12, array, 0);
        System.out.println(array);
    }
}

A. My_Java_example, Ja, J_
Myva_ex_example
B. My_Java_example, av, ava_
My_va_exexample
C. My_Java_example, Ja, Java
Ma_exam_example
D. My_Java_example, Java, Java_e
va_exam_example
E. Compilation Error or Runtime Error
Topic: Unknown

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 since substr(3,4) selects string java from char array similarly other str functions selects from char array and prints

Posted by Bharath Yelchuri    2014-03-04 09:15:38


D is answer since array indexes starts from 0 1 2........n.the char array stores all the characters given.first it prints all the chars by copyvalue to str and prints then substr selects java in array then prints next substr takes java_e and getchars takes next va_exam.example and prints(all are java string functions)

Posted by Naga Sinst    2014-03-04 10:09:48


ans: D..because when we store the(we know that array index is starts with zero) characters in to the array of char.and then those to be copied in to the string sequentially without any gap.so first printing it will display the array like My_Java_example and ,  is appended in that sop statement********..next. in that print statement public static String copyValueOf(char[] data,
                                 int offset,
                                 int count)..so we are using character array elements at 3,4,5,6 positions..so it will print Java ***********and then next print statement 3,4,5,6,7,8 positions of character array so Java_eprinted********and next sop is used getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)..so va_exam_example to be printed in the output.

Posted by Sapparapu Pradeep Kumar    2014-03-04 10:18:49


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

Posted by Merit Campus    2014-03-05 06:18:33


© meritcampus 2019

All Rights Reserved.

Open In App