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 Coding {
    public static void main(String[] args) {
        byte name[] = {65, 108, 98, 101, 114, 116, 32, 69, 105, 110, 115, 116, 101, 105, 110};
        String decode = new String(name);
        decode = new String(name, 7, name.length - 6);

A. 65108981011141163269105110115116101105110
B. Albert Einstein
C. Some other output 
D. Compilation Error 
E. Runtime Error 
Topic: Java String

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.

The answer is E since in the assignment of decode with the String(name,7,name.length-6) the string goes out of bounds, as the byte array offset is given as 7 and the length is subtracted by 6. This is an invalid index

Posted by Divya Shritha    2014-05-20 04:06:31

ans d.1st output prints albert Einstein .2nd decode operation throws exception stringindexoutofbound -compilr err

Posted by Maheshwari Natarajan    2014-05-20 04:08:05

> the Statement decode = new String(name , 7 , name.length-6)
// String name, startIndex , Length//
will throw a error coz we are trying to access a STRING OUT OF ITS BOUNDS...If it was name.lenght-7 we would get Option : B

Posted by Deepak Kumar Yadav    2014-05-20 04:13:20

Ans is B.. byte values vl be taken as characters so it Albert EinStein first and then subString the decode..

Posted by Om Prakash Om    2014-05-20 04:15:07

Ans is e It will give runtime exception.while  executing String(name,7,name.length - 6) sub array index starts at 7 th index position    and it has to print 9(15(array length)-6) characters which results in out of range for the array.It will give String Index Out Of bound exception

Posted by Pranavi Prakash    2014-05-20 04:28:31

Runtime Error..... Pranavi  good ans ....

Posted by Krishna Reddy    2014-05-20 05:07:12

Congratulations Deepak Kumar Yadav. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-21 04:10:09

© meritcampus 2019

All Rights Reserved.

Open In App