Menu
Topics Index
...
`

Session 17A has these questions


I Am Great

What will be the output of the following program?
public class IAmGreat {
    public static void main(String args[]) {
        int i = 132;
        short s = 15;
        byte b = (byte) i;
        int x = b + s;
        System.out.println("Value of x is " + x);
    }
}
A. Value of x is 147
B. Value of x is -109
C. Compilation Error
D. Runtime Error



Find the value of b at the end of the program

What will be the value of b at the end of the program?
class IntegerConversion
{

    public static void main(String args[])
    {
        int i = 50000;
        byte b = i;
    }
}
A. b will be 50000.
B. b will be between -32787 and 32786.
C. b will be between -128 to 127.
D. Program does not compile.



Integer conversion output106

What will be the output of the following program?
class IntegerConversion
{
    public static void main(String args[])
    {
        long l = 55;
        int i = 44;
        short s = 33;
        byte b    = 22;
        
        i = l;
        s = i;
        b = s;

        System.out.println("l = " + l);
        System.out.println("i = " + i);
        System.out.println("s = " + s);
        System.out.println("b = " + b);
    }
}
A. l = 55
i = 44
s = 33
b = 22
B. l = 22
i = 33
s = 44
b = 55
C. l = 55
i = 55
s = 55
b = 55
D. Program does not compile.



Print character variables after conversion

What will be the output of the following program?
class CharDemo
{
    public static void main(String args[])
    {
        char ch1 = 88; // 88 is character code of X
        char ch2 = 'Y'; // Y character code is 89
        
        int i1 = ch1;
        
        System.out.println("i1 = " + i1);
        System.out.println("ch1 = " + ch1);
        System.out.println("ch2 = " + ch2);
    }
}
A. i1 = 88
ch1 = X
ch2 = Y
B. i1 = X
ch1 = X
ch2 = Y
C. Compilation errors.
D. i1 = 88
ch1 = 88
ch2 = 89



TypeCasting values

What will be the output of the following program?
public class DemoOnOverLoad
{
    public static void main(String[] args)
    {
        int a = 99;
        System.out.println((Long) a);
    }
}
A. 99
B. 99L
C. 99l
D. -99L
E. Compilation Error or Runtime Error



Floating point variables conversion110

What will be the output of the following program?
class FloatConversion
{
    public static void main(String args[])
    {
        int i = 44;
        float f = 98.42f;
        double d = 103.67;
        
        f = (float) i;
        d = (double) f;
        i = (int) d;

        System.out.println("i = " + i);
        System.out.println("f = " + f);
        System.out.println("d = " + d);
    }
}
A. i = 44
f = 44.0
d = 44.0
B. i = 103.67
f = 44
d = 98.42
C. Compilation error since the initialization of double d is wrong. It should be 103.67d.
D. Compilation errors since we are casting integer to float and float to double.



© meritcampus 2019

All Rights Reserved.

Open In App