Menu
Topics Index
...
`

Print Bytes of a String1183


If you need explanation Read this topic

If you need Answer Take test on this topic

Print Bytes of a String1183
What will be the output of the following program?
public class ByteExample
{
    public static void main(String[] args)
    {
        String str = "BCDEFG";
        byte[] bytes = str.getBytes();
        for (byte byt : bytes)
        {
            System.out.print((char) byt + " ");
            byt--;
        }
    }
}

A. B C D E F G
B. A B C D E F
C. B B C D E F
D. B B B B B B
E. Program goes into infinite loop
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.

here ans is A
here in the main() method a string variable naming as str is assigned with data

i.e.... String str="BCDEFG";

here we are using getBytes() method which encodes the string variable into a sequence of bytes using default charset and stores the data into the new byte array
i.e..str.getBytes();

byte[] by=str.getBytes();

and here we are using for each loop for printing the values
        for(byte byt:by)
        {
            System.out.println((char)byt+" ");
            byt--;
        }

in the byt array which is type casted into character type exclusively...
and the for each loop continuos till the byt each the end of array length and which is decremented by one in th body..

Posted by Vicky Maduri    2015-02-26 14:38:03


ANS IS A

Here the concept of TYPECASTING is used.

--Here, first string is declared with content BCDEFG

--After that string is converted into bytes by using getbytes() method and it returns byte array containing ASCII values of each character in bytes.

---After that, for loop is used for each byte value and it runs till the length of bye array.

Byte is then typecasted to character. So,for each byte character will be displayed separated by space.

---So, eventually output will be characters of string separated by space.

OUTPUT:
B C D E F G

Posted by Mânïshå Mùlchåndânï    2015-02-26 16:22:46


Ans is A..her we have the Bytes Concept and we create the String with the literal "BCDEFG" which will store in the SP..next we are calling the getBytes() on the String which will returns the bytes sequence for the each char in the given string, here it will return the bytes array for char in the given String
Execution:
here we are getting the array of the Bytes for the BCDEFG so here it will return bytes array and next by using the for each loop we travelling through the array while printing the data we are type converting the byte to the chat explicitly so here it will print the char for related ASCII value in the Array

Posted by Uday Kumar    2015-02-26 17:04:34


a

Posted by Shubham Bansal    2015-02-26 19:13:34


Answer is a

Posted by Shashanka Mogaliraju    2015-02-27 03:01:43


This dose is now closed and the winners are Vicky Maduri,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Vicky Maduri. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-27 03:58:24


© meritcampus 2019

All Rights Reserved.

Open In App