Menu
Topics Index
...
`

Print String Bytes in Range


If you need explanation Read this topic

If you need Answer Take test on this topic

Print String Bytes in Range
What will be the output of the following program?
public class ByteExample1
{
    public static void main(String[] args)
    {
        String str = "Merit_";
        byte[] bytes = str.getBytes();
        for (byte byt : bytes)
        {
            byte b5 = (byt > 65 && byt < 90) ? byt : (byt > 97 && byt < 122) ? byt : (int) 'X';
            System.out.print(b5 + " ");
        }
    }
}

A. 77 101 114 105 116 X
B. M e r i t 88
C. 77 101 114 105 116 88
D. Compilation Error because int can not be assigned to byte
E. Compilation Error or Runtime Error for some other reason
Topic: Java Character Extraction - Java String getBytes() Method

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 d int cant assignd to byte
compilation err at int 'x' assigned to byte b5 .fr underscore

Posted by Maheshwari Natarajan    2014-09-05 08:35:52


ans is C in the line byte[] bytes=str.getBytes()Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

Posted by Ramya Reddy Addulla    2014-09-05 08:40:44


ans ia A  ,,bcoz int('_')=95

Posted by Raju VarshnEy    2014-09-05 08:45:03


answer a.
for Merit it prints ascii of each char.
underscore ascii value 95.so conditions faild thn it assigns b5 wit x

Posted by NagaRaj Nataraj    2014-09-05 09:36:07


c..byte enodes the strng into a sequence ofbytes using the platform's default charset,storing the result into a new byte array.

Posted by Wajahat Khan    2014-09-05 13:44:53


Congratulations Ramya Reddy Addulla. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-09-08 04:49:14


© meritcampus 2019

All Rights Reserved.

Open In App