Menu
Topics Index
...
`

BinaryFormat


If you need explanation Read this topic

If you need Answer Take test on this topic

BinaryFormat
What will be the output of the following program?
public class PrintStatment10
{
    public static void main(String args[])
    {
        PrintStatment10 obj = new PrintStatment10();
        obj.printBinaryFormat(23);
    }
    public void printBinaryFormat(int number)
    {
        int binary[] = new int[25];
        int index = 0;
        while (number > 0)
        {
            binary[index++] = number % 2;
            number = number / 2;
        }
        for (int i = index - 1; i >= 0; i--)
        {
            System.out.print(binary[i]);
        }
    }
}

A. 11101
B. 10111
C. 11001
D. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

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 B:Because number will convert into binaryformate

Posted by Ashok Reddi    2014-02-19 08:56:57


Answer is B. printBinaryFormat method prints out the binary equivalent of decimal number passed to it. Here the binary array stores the resulting binary number in reverse. So we traverse through the array from the end to the beginning.

Posted by Shaileshwar Sharma    2014-02-19 09:13:12


Ans: B first the value 23 >1..true ..so it goes to the while loop..and then the remainder values to be stored in the sequence of bytes in binary array..so the values are store in the 11101.and then while printing it will be print in reverse order in the for loop like 10111.so answer is.... B

Posted by Sapparapu Pradeep Kumar    2014-02-19 10:07:35


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

Posted by Merit Campus    2014-02-20 04:23:02


© meritcampus 2019

All Rights Reserved.

Open In App