Menu
Topics Index
...
`

Type Casting Variables1409


If you need explanation Read this topic

If you need Answer Take test on this topic

Type Casting Variables1409
What will be the output of the following program?
public class Conversions
{
    public static void main(String args[])
    {
        byte x;
        int a = 270;
        double b = 128.128;
        x = (byte) a;
        System.out.println("a and x " + a + " " + x);
        a = (int) b;
        System.out.println("b and a " + b + " " + a);
        x = b;
        System.out.println("b and x " + b + " " + x);
    }
}

A. a and x 270 14
b and a 128.128 128
b and x 128.128 -128
B. a and x 270 14
b and x 128.128 -128
b and a 128.128 128
C. a and x 270.0 14.0
b and a 128.128 128
b and x 128.128 -128
D. Compilation Error or Runtime Error
Topic: Type Casting In Java

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 byte takes 1 byte of memory and double takes 8 bytes of memory. Assigning 8 bytes of memory to 1 byte of memory requires "explicit" casting. So, the statement x = b causes a compilation error, "possible loss of precision". Explicitly typecasting double b to byte x would fix the error ----> x = (byte) b

Posted by Shaileshwar Sharma    2014-02-28 10:07:17


No winner for this dose. Correct option is 'D' :(. Shaileshwar Sharma was already won in this week.

Posted by Merit Campus    2014-03-03 09:19:30


© meritcampus 2019

All Rights Reserved.

Open In App